본문 바로가기
프로그래밍/개발 언어

Vue v-bind 개념

by monicada 2022. 12. 13.
728x90

html속성을 vue data를 이용하여 관리하는 경우 'v-bind'를 사용한다 

마지막 줄의 'about Vue'를 클릭하면 app.js에서 지정한 url로 이동한다 

// app.js

const app = Vue.createApp({
    data() {
        return {
            courseGoal: 'Finish the course and learn Vue!',
            vueLink: 'https://vuejs.org/'
        };
    }
});

app.mount('#user-goal');
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Basics</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <script src="https://unpkg.com/vue@next" defer></script>
    <script src="app.js" defer></script>
  </head>
  <body>
    <header>
      <h1>Vue Course Goals</h1>
    </header>
    <section id="user-goal">
      <h2>My couse Goal</h2>
      <p>{{ courseGoal }}</p>
      <p>Learn more <a v-bind:href="vueLink">about Vue</a>.</p>
    </section>
  </body>
</html>
/* styles.css */

* {
  box-sizing: border-box;
}

html {
  font-family: 'Jost', sans-serif;
}

body {
  margin: 0;
}

header {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
  margin: 3rem auto;
  border-radius: 10px;
  padding: 1rem;
  background-color: #4fc08d;
  color: white;
  text-align: center;
  width: 90%;
  max-width: 40rem;
}

#user-goal {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
  margin: 3rem auto;
  border-radius: 10px;
  padding: 1rem;
  text-align: center;
  width: 90%;
  max-width: 40rem;
}

#user-goal h2 {
  font-size: 2rem;
  border-bottom: 4px solid #ccc;
  color: #4fc08d;
  margin: 0 0 1rem 0;
}

#user-goal p {
  font-size: 1.25rem;
  font-weight: bold;
  border: 1px solid #4fc08d;
  background-color: #4fc08d;
  color: white;
  padding: 0.5rem;
  border-radius: 25px;
}

#user-goal input {
  font: inherit;
  border: 1px solid #ccc;
}

#user-goal input:focus {
  outline: none;
  border-color: #1b995e;
  background-color: #d7fdeb;
}

#user-goal button {
  font: inherit;
  cursor: pointer;
  border: 1px solid #ff0077;
  background-color: #ff0077;
  color: white;
  padding: 0.05rem 1rem;
  box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.26);
}

#user-goal button:hover,
#user-goal button:active {
  background-color: #ec3169;
  border-color: #ec3169;
  box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.26);
}

추가적으로, 함수형식으로 만드는 방법은 methods를 사용하는 것이다 

// app.js

const app = Vue.createApp({
    data() {
        return {
            courseGoal: 'Finish the course and learn Vue!',
            vueLink: 'https://vuejs.org/'
        };
    },
    methods: {
        outputGoal() {
            const randomNumber = Math.random();
            if (randomNumber < 0.5) {
                return 'Learn Vue!';
            } else {
                return 'Master Vue!';
            }
        }
    },
});

app.mount('#user-goal');

이런 식으로 변경하면 코드를 동적으로 관리할 수 있다 

 

데이터를 관리하는 방법은 자바스크립트의 'this'함수를 이용하는 방식이 있다

this.()를 사용하면 데이터를 가져와서 사용할 수 있다

원리: data객체에서 반환하는 데이터 전체를 가져가다 데이터를 병합하여 전역 Vue 인스턴스 객체를 만듦

// app.js

const app = Vue.createApp({
    data() {
        return {
            courseGoalA: 'Finish the course and learn Vue!',
            cousrseGoalB: 'Master Vue and build amazing apps!',
            vueLink: 'https://vuejs.org/'
        };
    },
    methods: {
        outputGoal() {
            const randomNumber = Math.random();
            if (randomNumber < 0.5) {
                return this.courseGoalA;
            } else {
                return this.courseGoalB;
            }
        }
    },
});

app.mount('#user-goal');
 

'프로그래밍 > 개발 언어' 카테고리의 다른 글

Vue 내부 원리  (0) 2022.12.15
Vue v-on 개념  (0) 2022.12.14
Vue 개념 정리  (0) 2022.12.12
Vue.js 공식문서 Tutorial  (0) 2022.12.11
Next.js 터미널 코드(npx create-next-app@latest를 곁들인..)  (0) 2022.12.08

댓글