使用data()中的数据
示例
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>
<script src="https://unpkg.com/vue@3/dist/vue.global.js" defer></script>
<script src="app.js" defer></script>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<header>
<h1>Vue Course Goals</h1>
</header>
<section id="user-goal">
<h2>My Course Goal</h2>
<!-- <p>{{courseGoal}}</p> -->
<p>{{outputGoal()}}</p>
<p>Learn more <a v-bind:href="vueLink">about Vue</a>.</p>
</section>
</body>
</html>
app.js
const app = Vue.createApp({
data() {
return {
courseGoalA: 'Finish the course and learn Vue!',
courseGoalB: 'Master Vue and build amazing apps!',
vueLink: 'https://cn.vuejs.org/'
}
},
methods: {
outputGoal() {
const randomNumber = Math.random();
if (randomNumber < 0.5) {
return this.courseGoalA;
} else {
return this.courseGoalB;
}
}
}
});
app.mount('#user-goal');