v-model双向绑定
v-bind单向绑定
v-model双向绑定的例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>v-model数据双向绑定练习</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- vue-app的根容器 -->
<div id="app">
<input type="text" placeholder="请输入" v-model="name"/>
<h2>你好,{{name}}</h2>
</div>
<script type="text/javascript">
/* 实例化一个Vue对象 */
var app = new Vue({
el: '#app',
data: {
name:'aaaa'
}
})
</script>
</body>
</html>
-
运行结果
使用v-bind绑定data中的属性
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>v-bind练习</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- vue-app的根容器 -->
<div id="app">
<p>姓名:{{name}}</p>
<input type="text" v-bind:value="description" />
<a v-bind:href="url">{{website}}</a>
<img :src="avatar" >
</div>
<script type="text/javascript">
/* 实例化一个Vue对象 */
var app = new Vue({
el: '#app',
data: {
name: '尤雨溪',
description: 'Vue.js的创建者',
website: 'Vue官网',
url: 'https://cn.vuejs.org/v2/guide/',
avatar: 'https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=c08a0c60a844ad342ebf8081e8996bc9/4afbfbedab64034f29596c8ba6c379310b551da2.jpg'
}
})
</script>
</body>
</html>
语法糖:v-bind可以简写为:,如<img :src="avatar"/>
-
运行结果