官方文档:https://cli.vuejs.org/zh/guide/creating-a-project.html#vue-create
## 查看@vue/cli版本,确保@vue/cli版本在4.5.0以上
vue --version
## 安装或者升级你的@vue/cli
npm install -g @vue/cli
## 创建
vue create vue_test
## 启动
cd vue_test
npm run serve
官方文档:https://v3.cn.vuejs.org/guide/installation.html#vite
vite官网:https://vitejs.cn
## 创建工程
npm init vite-app
## 进入工程目录
cd
## 安装依赖
npm install
## 运行
npm run dev
(1)src/main.js
分析
// 引入的不再是Vue构造函数,引入的是一个名为createApp的工厂函数
import { createApp } from 'vue'
import App from './App.vue'
// 创建应用实例对象——app(类似于之前Vue2中的vm,但app比vm更‘轻’)
// 将createApp(App).mount('#app')变形
const app = createApp(App)
// 挂载
app.mount('#app')
// 卸载
// app.unmount('#app')
// createApp(App).mount('#app')
// Vue2写法
// const vm = new Vue({
// render:h=>h(App)
// }).$mount('app')
(2)Vue3组件中的模板结构可以没有跟标签
<template>
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</template>
官方文档: https://v3.cn.vuejs.org/guide/composition-api-introduction.html
Composition API(组合API)
“ 表演的舞台 ”。src/App.vue
<template>
<h1>一个人的信息</h1>
<h2>姓名:{{ name }}</h2>
<h2>年龄:{{ age }}</h2>
<h2>性别:{{ sex }}</h2>
<button @click="sayHello">说话(vue3配置的——sayHello)</button>
<br />
<br />
<button @click="sayWelcome">说话(vue2配置的——sayWelcome)</button>
</template>
<script>
import { h } from "vue";
export default {
name: "App",
data() {
return {
sex: "男",
};
},
methods: {
sayWelcome() {
alert("欢迎!");
},
},
setup() {
let name = "张三";
let age = 18;
function sayHello() {
alert(`我叫${name},我今年${age}岁了,你好呀!`);
}
// 返回一个对象(常用)
return {
name,
age,
sayHello,
};
// 返回一个函数(渲染函数)
// return () => h("h1", "你好!");
},
};
</script>
src/App.vue
<template>
<h1>一个人的信息</h1>
<h2>姓名:{{ name }}</h2>
<h2>年龄:{{ age }}</h2>
<h3>工作种类:{{ job.type }}</h3>
<h3>工作薪资:{{ job.salary }}</h3>
<button @click="updateInfo">修改人的信息</button>
</template>
<script>
import { ref } from "vue";
export default {
name: "App",
setup() {
let name = ref("张三");
let age = ref(18);
let job = ref({
type: "前端工程师",
salary: "30k",
});
function updateInfo() {
name.value = "李四";
age.value = 28;
job.value.type = "后端工程师";
job.value.salary = "50k";
}
// 返回一个对象(常用)
return {
name,
age,
job,
updateInfo,
};
},
};
</script>
src/App.vue
<template>
<h1>一个人的信息</h1>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<h3>工作种类:{{ person.job.type }}</h3>
<h3>工作薪资:{{ person.job.salary }}</h3>
<h3>爱好:{{ person.hobby }}</h3>
<h3>测试的数据c:{{ person.job.a.b.c }}</h3>
<button @click="updateInfo">修改人的信息</button>
</template>
<script>
import { reactive } from "vue";
export default {
name: "App",
setup() {
let person = reactive({
name: "张三",
age: 18,
job: {
type: "前端工程师",
salary: "30k",
a: {
b: {
c: 666,
},
},
},
hobby: ["篮球", "打游戏", "睡觉"],
});
function updateInfo() {
person.name = "李四";
person.age = 28;
person.job.type = "后端工程师";
person.job.salary = "50k";
person.job.a.b.c = 999;
person.hobby[2] = "吃饭";
}
// 返回一个对象(常用)
return {
person,
updateInfo,
};
},
};
</script>
Vue.set(target,propertyName/index,value)
vm.$set(target,propertyName/index,value)
Vue.delete(target,propertyName/index,value)
vm.$delete(target,propertyName/index,value)
new Proxy(data, {
// 拦截读取属性值
get (target, prop) {
return Reflect.get(target, prop)
},
// 拦截设置属性值或添加新属性
set (target, prop, value) {
return Reflect.set(target, prop, value)
},
// 拦截删除属性
deleteProperty (target, prop) {
return Reflect.deleteProperty(target, prop)
}
})
proxy.name = 'tom'
src/App.vue
<template>
<Demo @hello="showHello" msg="你好呀" school="昆明">
<template v-slot:gqe>
<span>耿耿</span>
</template>
</Demo>
</template>
<script>
import Demo from "./components/Demo";
export default {
name: "App",
components: {
Demo,
},
setup() {
function showHello(value) {
alert(`你出发了hello事件,我收到的参数值是:${value}`);
}
return {
showHello,
};
},
};
</script>
src/components/Demo.vue
<template>
<h1>一个人的信息</h1>
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<slot name="gqe"></slot>
<button @click="test">测试触发了Demo组件的hello事件</button>
</template>
<script>
import { reactive } from "vue";
export default {
name: "Demo",
props: ["msg", "school"],
emits: ["hello"],
setup(props, context) {
console.log("props", props); //Proxy {msg: '你好呀', school: '昆明'}
console.log("context", context);
let person = reactive({
name: "张三",
age: 18,
});
function test() {
context.emit("hello", 666);
}
// 返回一个对象(常用)
return {
person,
test,
};
},
};
</script>
import {computed} from 'vue'
setup(){
...
//计算属性——简写
let fullName = computed(()=>{
return person.firstName + '-' + person.lastName
})
//计算属性——完整
let fullName = computed({
get(){
return person.firstName + '-' + person.lastName
},
set(value){
const nameArr = value.split('-')
person.firstName = nameArr[0]
person.lastName = nameArr[1]
}
})
}
<template>
<h1>当前求和为:{{ sum }}</h1>
<button @click="sum++">点我加一</button>
<hr />
<h2>当前信息为:{{ msg }}</h2>
<button @click="msg += '!'">点我给信息加!</button>
<hr />
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<h2>薪资:{{ person.job.j1.salary }}k</h2>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age++">修改年龄</button>
<button @click="person.job.j1.salary++">涨薪</button>
</template>
<script>
import { ref, reactive, watch } from "vue";
export default {
name: "Demo",
setup() {
let sum = ref(0);
let msg = ref("你好啊");
let person = reactive({
name: "张三",
age: 18,
job: {
j1: {
salary: 20,
},
},
});
//情况一:监视ref所定义的一个响应式数据
watch(
sum,
(newValue, oldValue) => {
console.log("sum变了", newValue, oldValue);
},
{ immediate: true }
);
//情况二:监视ref所定义的多个响应式数据
watch([sum, msg], (newValue, oldValue) => {
console.log("sum或msg变了", newValue, oldValue);
});
/* 情况三:监视reactive定义的响应式数据
若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!!
若watch监视的是reactive定义的响应式数据,则强制开启了深度监视
*/
watch(
person,
(newValue, oldValue) => {
console.log("person变了", newValue, oldValue);
},
{ immediate: true, deep: false }
); //此处的deep配置不再奏效
//情况四:监视reactive定义的响应式数据中的某个属性
watch(
() => person.age,
(newValue, oldValue) => {
console.log("person的age变了", newValue, oldValue);
}
);
//情况五:监视reactive定义的响应式数据中的某些属性
watch([() => person.age, () => person.name], (newValue, oldValue) => {
console.log("person的name或age变了", newValue, oldValue);
});
//特殊情况
watch(
() => person.job,
(newValue, oldValue) => {
console.log("person的job变化了", newValue, oldValue);
},
{ deep: true }
); //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效
return {
sum,
msg,
person,
};
},
};
</script>
<template>
<h1>当前求和为:{{ sum }}</h1>
<button @click="sum++">点我加一</button>
<hr />
<h2>当前信息为:{{ msg }}</h2>
<button @click="msg += '!'">点我给信息加!</button>
<hr />
<h2>姓名:{{ person.name }}</h2>
<h2>年龄:{{ person.age }}</h2>
<h2>薪资:{{ person.job.j1.salary }}k</h2>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age++">修改年龄</button>
<button @click="person.job.j1.salary++">涨薪</button>
</template>
<script>
import { ref, reactive, watch } from "vue";
export default {
name: "Demo",
setup() {
let sum = ref(0);
let msg = ref("你好啊");
let person = ref({
name: "张三",
age: 18,
job: {
j1: {
salary: 20,
},
},
});
// watch监听ref定义的基本数据类型数据,不需要.value
watch(sum, (newValue, oldValue) => {
console.log("sum变了", newValue, oldValue);
});
// watch监听ref定义的对象数据
// 方法一:用.value的形式
watch(person.value, (newValue, oldValue) => {
console.log("person变了", newValue, oldValue);
});
// 方法二:用深度监视
watch(
person,
(newValue, oldValue) => {
console.log("person变了", newValue, oldValue);
},
{ deep: true }
);
return {
sum,
msg,
person,
};
},
};
</script>
//watchEffect所指定的回调中用到的数据只要发生变化,则直接重新执行回调。
watchEffect(()=>{
const x1 = sum.value
const x2 = person.age
console.log('watchEffect配置的回调执行了')
})
setup() {
let sum = ref(0);
onBeforeMount(() => {
console.log("---onBeforeMount---");
});
onMounted(() => {
console.log("---onMounted---");
});
onBeforeUpdate(() => {
console.log("---onBeforeUpdate---");
});
onUpdated(() => {
console.log("---onUpdated---");
});
onBeforeUnmount(() => {
console.log("---onBeforeUnmount---");
});
onUnmounted(() => {
console.log("---onUnmounted---");
});
return {
sum,
};
},
src/App.vue
<template>
<button @click="isShowDemo = !isShowDemo">切换隐藏/显示</button>
<Demo v-if="isShowDemo" />
</template>
<script>
import Demo from "./components/Demo";
import { ref } from "vue";
export default {
name: "App",
components: {
Demo,
},
setup() {
let isShowDemo = ref(true);
return {
isShowDemo,
};
},
};
</script>
src/components/Demo.vue
<template>
<h1>当前求和为:{{ sum }}</h1>
<button @click="sum++">点我加一</button>
<hr />
<h2>当前点击时鼠标的坐标:x:{{ point.x }},y:{{ point.y }}</h2>
</template>
<script>
import { ref } from "vue";
import usePoint from "../hooks/usePoint";
export default {
name: "Demo",
setup() {
let sum = ref(0);
let point = usePoint();
return {
sum,
point,
};
},
};
</script>
src/hooks/usePoint.js
import { reactive, onMounted, onBeforeUnmount } from "vue";
export default function () {
let point = reactive({
x: 0,
y: 0,
});
function savePoint(event) {
point.x = event.pageX;
point.y = event.pageY;
console.log(point.x, point.y);
}
onMounted(() => {
window.addEventListener("click", savePoint);
});
onBeforeUnmount(() => {
window.removeEventListener("click", savePoint);
});
return point
}
<template>
{{ person }}
<h2>姓名:{{ name }}</h2>
<h2>年龄:{{ age }}</h2>
<!-- toRef -->
<!-- <h2>toRef薪资:{{ salary }}k</h2> -->
<!-- toRefs-->
<h2>toRefs薪资:{{ job.j1.salary }}k</h2>
<button @click="name += '~'">修改姓名</button>
<button @click="age++">修改年龄</button>
<!-- toRef -->
<!-- <button @click="salary++">toRef涨薪</button> -->
<!-- toRefs -->
<button @click="job.j1.salary++">toRefs涨薪</button>
</template>
<script>
import { reactive, toRef, toRefs } from "vue";
export default {
name: "Demo",
setup() {
let person = reactive({
name: "张三",
age: 18,
job: {
j1: {
salary: 20,
},
},
});
return {
person,
// name: toRef(person, "name"),
// age: toRef(person, "age"),
// salary: toRef(person.job.j1, "salary"),
...toRefs(person),
};
},
};
</script>
let person = reactive({
name: "张三",
age: 18,
job: {
j1: {
salary: 20,
},
},
});
person = readonly (person)
<template>
<input type="text" v-model="keyword">
<h3>{{keyword}}</h3>
</template>
<script>
import {ref,customRef} from 'vue'
export default {
name:'Demo',
setup(){
// let keyword = ref('hello') //使用Vue准备好的内置ref
//自定义一个myRef
function myRef(value,delay){
let timer
//通过customRef去实现自定义
return customRef((track,trigger)=>{
return{
get(){
track() //告诉Vue这个value值是需要被“追踪”的
return value
},
set(newValue){
clearTimeout(timer)
timer = setTimeout(()=>{
value = newValue
trigger() //告诉Vue去更新界面
},delay)
}
}
})
}
let keyword = myRef('hello',500) //使用程序员自定义的ref
return {
keyword
}
}
}
</script>
setup(){
......
let car = reactive({name:'奔驰',price:'40万'})
provide('car',car)
......
}
setup(props,context){
......
const car = inject('car')
return {car}
......
}
使用传统OptionsAPI中,新增或者修改一个需求,就需要分别在data,methods,computed里修改 。(Vue2)
我们可以更加优雅的组织我们的代码,函数。让相关功能的代码更加有序的组织在一起。(Vue3)
什么是Teleport?—— Teleport 是一种能够将我们的组件html结构移动到指定位置的技术。
<teleport to="移动位置">
<div v-if="isShow" class="mask">
<div class="dialog">
<h3>我是一个弹窗</h3>
<button @click="isShow = false">关闭弹窗</button>
</div>
</div>
</teleport>
import {defineAsyncComponent} from 'vue'
const Child = defineAsyncComponent(()=>import('./components/Child.vue'))
我是App组件
加载中.....
//注册全局组件
Vue.component('MyButton', {
data: () => ({
count: 0
}),
template: ''
})
//注册全局指令
Vue.directive('focus', {
inserted: el => el.focus()
}
.v-enter,
.v-leave-to {
opacity: 0;
}
.v-leave,
.v-enter-to {
opacity: 1;
}
.v-enter-from,
.v-leave-to {
opacity: 0;
}
.v-leave-from,
.v-enter-to {
opacity: 1;
}
父组件中绑定事件
子组件中声明自定义事件
过滤器虽然这看起来很方便,但它需要一个自定义语法,打破大括号内表达式是 “只是 JavaScript” 的假设,
这不仅有学习成本,而且有实现成本!建议用方法调用或计算属性去替换过滤器。