vue入门教程2
vue入门学习笔记
工具安装
vscode
1、vue语法
mvvm模式

1.1 编写案例helloworld count计数器
最简单的一个demo

计算器demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data(){
return {
content: 1
}
},
mounted(){
console.log('mounted') //当页面加载完,这个函数会自动执行
},
template: '<div>{{content}}</div>'
}).mount('#root')
</script>
</html>
完成加载器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data(){
return {
content: 1
}
},
mounted(){
setInterval(()=>{
this.content += 1; //指向的是 this.$data.content,就是data中的content
//手动就是document.getElementById('root').innerHTML(2) 现在就是不需要这样操作了
//之前就是面向dom编程,现在就是面向数据编程
}, 1000)
},
template: '<div>{{content}}</div>'
}).mount('#root')
</script>
</html>
1.2 事件绑定的使用
写一个字符串反转的demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data() {
return {
content: "hello, world"
}
},
methods: {
handleBtnClick(){
this.content = this.content.split('').reverse().join('');
}
},
template: `
<div>
{{content}}
<button v-on:click="handleBtnClick">反转</button>
</div>
`
}).mount('#root')
</script>
</html>
写一个内容隐藏的小功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data() {
return {
content: "hello, world",
show: true
}
},
methods: {
handleBtnClick(){
this.show = !this.show
}
},
template: `
<div>
<span v-if="show"> {{content}} </span>
<button v-on:click="handleBtnClick">显示/隐藏</button>
</div>
`
}).mount('#root')
</script>
</html>
1.3 v-mode的一些使用
循环的一些功能使用和简单的todolist
列表循环展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data() {
return {
content: "hello, world",
show: true,
list: ["hello","wold","dell","lee"]
}
},
template: `
<ul>
<li v-for="(item,index) of list">{{item}} {{index+1}}</li>
</ul>
`
}).mount('#root')
</script>
</html>
push数据
<v-model中的inputValue跟data中的inputValue做了绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello, world!</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
Vue.createApp({ //创建Vue实例
data() {
return {
content: "hello, world",
show: true,
list: [],
inputValue: '',
}
},
methods: {
handleAdxdItem(){
console.log(this.inputValue)
this.list.push(this.inputValue)
this.inputValue='' //在input中输入后点击增加完自动清空输入中的内容
}
},
template: `
<div>
<input v-model="inputValue" />
<button v-on:click="handleAdxdItem">增加</button>
<ul>
<li v-for="(item,index) of list">{{item}} {{index+1}}</li>
</ul>
</div>
`
}).mount('#root')
</script>
</html>