Vue基础知识运用实例(一)

1、Vue第一个实例:Hello Vue.js!  el指向html元素

 

<!DOCTYPE html>

<html>

<head>

<meta charset =“utf-8”>

<title> Hello Vue.js!</ title>

<script src =“https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js”> </ script>

</ head>

<body>

<div id =“app”>

<P> {{message}} </ P>

</ div>

<script>

new Vue({

el:'#app',

data:{

message:'Hello Vue.js!'

}

})

</ script>

</ body>

</ html>

 

2、Vue第二个实例:双向数据绑定

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>双向数据绑定</title>

<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>

</head>

 

<body>

<div id="app">

<p>{{message}}</p>

<input v-model="message">

<!--用户更新了view,MVVM框架自动更新model的状态-->

</div>

<script>

new Vue({

el: '#app',

data: {

message: 'Fighting yao!'

}

})

</script>

</body>

</html>

 

3、Vue第三个实例:渲染列表

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>渲染列表</title>

<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>

</head>

 

<body>

<div id="app">

<ul>

<li v-for="todo in todos">

{{todo.text}}

</li>

</ul>

</div>

 

<script>

new Vue({

el: '#app',

data: {

todos: [{

text: 'Learn JavaScript'

}, {

text: 'Learn Vue.js'

}, {

text: 'Build Something Awesome'

}]

}

})

</script>

</body>

</html>

结果:

4、Vue第四个实例:处理用户输入

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>处理用户输入</title>

<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>

</head>

 

<body>

<div id="app">

<p>

{{message}}

</p>

<button v-on:click="reverseMessage">Reverse Message</button>

</div>

<script>

new Vue({

el: '#app',

data: {

message: 'Hello Vue.js!'

},

methods: {

reverseMessage: function() {

this.message = this.message.split('').reverse().join()

}

}

})

</script>

</body>

 

</html>

结果:

 

5、Vue第四个实例:添加一些待办事项

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>改变待办事项</title>

<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>

</head>

 

<body>

<div id="app">

<input v-model="newTodo" v-on:keyup.enter="addTodo">

<ul>

<li v-for="todo in todos">

<span>

{{todo.text}}

</span>

<button v-on:click="removeTodo($index)">X</button>

</li>

</ul>

</div>

<script>

new Vue({

el: '#app',

data: {

newTodo: '',

todos: [{

text: 'Add some todos'

}]

},

methods: {

addTodo: function() {

var text = this.newTodo.trim()

if (text) {

this.todos.push({

text: text

})

this.newTodo = ''

}

}

}

})

</script>

</body>

 

</html>

结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值