一、学习Vue3的相关资料
Vue3的官方说明书
关于Vue3的一切
div和span标签以及标签分类
关于JS的一切
关于Html的一切
关于Css的一切
二、Vue3课程笔记
基础Vue示例:hello world
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
massage:'hello world'
}
},
template:"<div>{{massage}}</div>"
});
app.mount('#root');
</script>
</html>
一、Vue中应用和组件的基础
1.设计模式
MVVM: m-> model数据 v->view 视图 vm->viewModel试图数据连接层
2. 应用
Vue.createApp()
:创建一个应用,每个 Vue 应用都是通过用 createApp 函数创建一个新的应用实例开始的const vm = app.mount()
: vm是获取应用的根组件,通过vm可以获取vue里的任何数据,属性和方法vm.$data
: 获取根组件数据对应内容massage:'hello world'
:定义数据template:"<div>{{massage}}</div>"
:定于模块mount()
:进行挂载,与上面内容进行联系data(){}
: data方法,用于在vue中声明数据,对象或函数类型,页面中可以直接访问使用methods:{}
: methods属性,用于编写vue中的方法,所有的方法由vue对象来调用, 访问data中的属性直接使用this.xx
二、生命周期函数
在某一时候会自动执行的函数
alert('<弹窗内容>')
:警告弹窗mounted()
:自动执行函数beforeCreate()
:在初始化/创建实例之前自动执行的函数created
:在初始化/创建实例之后自动执行的函数
三.表单中双向绑定指令的使用
- checkbox
(1)
-可多选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
message:[]
}
},
template:`
<div>
{{message}}
Bell <input type="checkbox" v-model="message" value="Bell" />
Amy<input type="checkbox" v-model="message" value="Amy" />
Pee <input type="checkbox" v-model="message" value="Pee" />
</div>
`
});
(2)自定义表示选择与否(不用true和false表示)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
message:"hello",
}
},
template:`
<div>
{{message}}
<input type="checkbox" v-model="message" true-value="hello" false-value="world" / >
</div>
`
});
const vm = app.mount('#root');
</script>
</html>
- radio
-单项选择
template:`
<div>
{{message}}
Bell <input type="radio" v-model="message" value="Bell" />
Amy<input type="radio" v-model="message" value="Amy" />
Pee <input type="radio" v-model="message" value="Pee" />
</div>
`
- select(opation)
(1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
message:''
}
},
template:`
<div>
{{message}}
<select>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</div>
`
});
const vm = app.mount('#root');
</script>
</html>
(2)
template:`
<div>
{{message}}
<select v-model="message">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</div>
`
(3)
template:`
<div>
{{message}}
<select v-model="message">
<option disable value=''>请选择内容</option>
<option value='A'>A</option>
<option value='B'>B</option>
<option value='C'>C</option>
</select>
</div>
`
(4)多选
//方法一:
const app = Vue.createApp({
data(){
return{
message:[]
}
},
template:`
<div>
{{message}}
<select v-model="message" multiple>
<option disable value=''>请选择内容</option>
<option value='A'>A</option>
<option value='B'>B</option>
<option value='C'>C</option>
</select>
</div>
`
});
//方法二:
const app = Vue.createApp({
data(){
return{
message:[],
options:[{
text:'A',value:'A'
},{
text:'B',value:'B'
},{
text:'C',value:'C'
}]
}
},
template:`
<div>
{{message}}
<select v-model="message" multiple>
<option disable value=''>请选择内容</option>
<option v-for="item in options" :value="item.value">{{item.text}}</option>
</select>
</div>
`
});
四.全局、局部组件
- 全局命名:小写字母,两个单词用-连接,如:hello-world
- 局部命名:首字母大写,驼峰命名,如:HelloWorld
全局组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
template:`
<div>
<hello />
<counter />
</div>
`
});
app.component('hello',{
template:`
<div>hello</div>
`
})
app.component('counter',{
data(){
return{
count:1
}
},
template:`
{{count}}
<button @click="count++">增加</button>
`
})
const vm = app.mount('#root');
</script>
</html>
局部组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const Counter={
data(){
return{
count:1
}
},
template:`
{{count}}
<button @click="count++">增加</button>
`
}
const HelloWorld={
template:`
<div>hello world</div>
`
}
const app = Vue.createApp({
components:{HelloWorld,Counter},
template:`
<div>
<HelloWorld />
<Counter />
</div>
`
});
const vm = app.mount('#root');
</script>
</html>
五、使用 Vue 实现基础的 CSS 过渡与动画效果
1.动画效果
<!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>Document</title>
<style>
@keyframes leftToRight{
0%{
transform: translateX(-100px);
}
50%{
transform: translateX(-50px);
}
0%{
transform: translateX(0px);
}
}
.animation{
animation: leftToRight 3s;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app=Vue.createApp({
data(){
return{
animate:{
animation:false
}
}
},
methods: {
handleClick(){
this.animate.animation = !this.animate.animation
}
},
template:`
<div>
<div :class="animate">hello world</div>
<button @click="handleClick">动画</button>
</div>
`
});
const vm=app.mount('#root');
</script>
</html>
2.过渡效果
<!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>Document</title>
<style>
.transition{
transition:3s background-color ease;
}
.blue{
background: blue;
}
.green{
background: green;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app=Vue.createApp({
data(){
return{
animate:{
transition:true,
blue:true,
green:false,
}
}
},
methods: {
handleClick(){
this.animate.blue = !this.animate.blue;
this.animate.green = !this.animate.green;
}
},
template:`
<div>
<div :class="animate" >hello world</div>
<button @click="handleClick">动画</button>
</div>
`
});
const vm=app.mount('#root');
</script>
</html>
六、状态动画
setInterval()
是一个实现定时调用的函数,可按照指定的周期(以毫秒计)来调用函数或计算表达式。
this.timerID = setInterval(
() => this.tick(),
1000
);
<!--清除该函数-->
clearInterval(this.timerID);
示例代码(如下):
<!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>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app=Vue.createApp({
data(){
return{
number:1,
animateNumber:1
}
},
methods: {
handleClick(){
this.number = 10;
if (this.animateNumber < this.number){
const animation = setInterval(() => {
this.animateNumber += 1;
if(this.animateNumber === 10){
clearInterval(animation);
}
},1000);
}
},
},
template:`
<div>
<div>
{{animateNumber}}
</div>
<button @click="handleClick">增加</button>
</div>
`
});
const vm=app.mount('#root');
</script>
</html>
七、Mixin混入的基础语法
mixin 混入
1.组件 data、methods 优先级高于 mixin data、methods 优先级
2.生命周期函数,先执行 mixin 里面的,在执行组件里面的
八、开发实现 Vue 中的自定义指令
示例1:自动聚焦
<!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>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app=Vue.createApp({
template:`
<div>
<input v-focus />
</div>
`
});
app.directive('focus',{
mounted(el){
el.focus();
}
})
const vm=app.mount('#root');
</script>
</html>