<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="app.js"></script>
index.html
<section id="user-goal">
<h2>My Course Goal</h2>
<p></p>
</section>
app.js
Vue.createApp({
...
}).mount("#user-goal")
app.js
Vue.createApp({
data() {
return {
courseGoal: 'Finish the course and learn Vue!'
};
}
}).mount("#user-goal")
index.html
<section id="user-goal">
<h2>My Course Goal</h2>
<p>{{ courseGoal }}</p>
</section>
<p>Learn more <a v-bind:href="vueLink">about Vue</a></p>
app.js
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 this.courseGoalA
} else {
return this.courseGoalB
}
}
}
}).mount("#user-goal")
this를 통해 data에 접근할수 있다.
이 외에도 메소드또한 this로 접근 가능하다.
Interpolation 과 같이 {{}} 안에서 호출할수 있다.
<p>{{ outputGoal() }}</p>
data() {
return {
courseGoalA: '<h2>Finish the course and study Vue!</h2>',
courseGoalB: '<h2>Finish the course and study Vue!</h2>',
vueLink: 'https://vuejs.org/'
};
},
<p v-html="outputGoal()"></p>
<button v-on:click="counter++">Add</button>
<button v-on:click="add()">Add</button>
<input type="text" v-on:input="setName">
setName(e) {
console.log(e)
this.name = e.target.value
}
<input type="text" v-on:input="setName($event)">
<form v-on:submit.prevent="submitForm">
<button v-on:click.right="reduce(5)">Subtract 5</button>
<button v-on:click.right.prevent="reduce(5)">Subtract 5</button>
<input type="text" v-on:input="setName($event)" v-on:keyup.enter="confirmInput">
<input type="text" v-model="name">
computed: {
fullName() {
if(this.name === "") {
return ""
}
return this.name + " " + "Karl"
}
}
watch: {
name(newValue, oldValue) {
if(newValue === '') {
this.fullName = '';
} else {
this.fullName = this.name + ' ' + this.lastName;
}
},
}
v-bind:style="{borderColor: boxSelected ? 'red' : 'gray'}"
- 선택 여부에 따라 border color를 변경하는 예시.
:class="boxSelected ? 'demo active' : 'demo'"
:class={
selected: boxSelected
}
:class="['demo', {selected: selectedBox}]"