Vue3 入门 Vue3 入门 Vue3 入门
setup的参数
setup(props, context) / setup(props, {attrs, slots, emit})
Demo_1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <div id ="app" > </div > <script src ="./vue.global.js" > </script > <script > const app = Vue.createApp({ setup(propos,context) { return { msg:"hello world" }; }, template:` <div > {{msg}} </div > ` }).mount("#app" ) </script >
Demo_2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <div id ="app" > <div > {{msg1}}___{{msg2}}___{{msg3}}___{{msg4}}</div > </div > <script src ="./vue.global.js" > </script > <script > const app = Vue.createApp({ setup(propos, context) { let msg1 = "hello world" ; let msg3 = Vue.ref("fei22" ); const {ref} = Vue; let msg4 = ref("fei33" ); return { msg1, msg2:"daFei" , msg3, msg4 }; }, }).mount("#app" ); </script >
ref
ref 是响应式
若果ref
定义的是复杂对象,内部会转为reactive
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <div id ="app" > <div > {{msg4}}</div > <button @click ="updateFei" > 更新</button > </div > <script src ="./vue.global.js" > </script > <script > const {ref} = Vue; const app = Vue.createApp({ setup(propos, context) { let msg4 = ref(1 ); function updateFei () { msg4.value = msg4.value + 1; } return { msg4, updateFei }; }, }).mount("#app" ); </script >
reactive reactive
里面的对象都是响应式, 添加对象也是响应式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <div id ="app" > <div > <p > {{msg4.name}}</p > <p > {{msg4.web.name3}}</p > </div > <button @click ="updateFei" > 更新</button > </div > <script src ="./vue.global.js" > </script > <script > const {reactive} = Vue; const app = Vue.createApp({ setup(propos, context) { let msg4 = reactive({ name:"daFei" , age: 18, web:{ name:"HTML" , name2:"CSS" , name3:"JavaScript" , } }); function updateFei () { msg4.name = msg4.name + "_1" ; msg4.web.name3 = msg4.web.name3 + "_1" ; } return { msg4, updateFei }; }, }).mount("#app" ); </script >
全局变量 1 2 3 4 5 6 7 8 9 app.config.globalProperties.$fei = "我是全局变量" ; import {onMounted, getCurrentInstance} from 'vue' getCurrentInstance().appContext.config.globalProperties.$fei const app = getCurrentInstance().appContext.config.globalProperties;const { proxy } = getCurrentInstance()
调用子组件方法 1 2 3 4 5 6 7 8 9 10 11 12 13 import { ref } from 'vue' const a = 1 const b = ref(2 )defineExpose({ a, b })
defineExpose
插槽
1 2 3 4 5 6 7 8 9 10 11 <!-- element-plus 步骤条,插槽使用--> <el-steps > <el-step title="Step 1" /> <el-step title="Step 2" > <template #description> <div> <span style="color: red">这里使用插槽 aaaaa</span> </div> </template> </el-step> </el-steps>
自定义hooks
函数 1 2 3 4 5 6 7 8 9 const useFeiHooks = () => { const foo = () => { console .log("使用hooks,调用成功" ); } return {foo} } const {foo} = useFeiHooks();
绑定CSS 1 2 3 4 5 6 7 <div :style="{background:`linear-gradient(${bar})`}"> <div style="width: 100px;height: 100px"></div> </div> <script setup> import {ref} from "vue"; const bar = ref("138deg, #3BECF2 0%, #4A84FF 100%"); // 背景渐变色 </script>
使用图标库 在Vue3
中使用FontAwesome
图标库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 npm i --save @fortawesome/vue-fontawesome@prerelease npm i --save @fortawesome/fontawesome-svg-core npm i --save @fortawesome/free-solid-svg-icons import { library } from "@fortawesome/fontawesome-svg-core" ;import { faPhone } from "@fortawesome/free-solid-svg-icons" ;library.add(faPhone); import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome" ;createApp(App).component("font-awesome-icon" , FontAwesomeIcon) .mount("#app" ); import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome" ;import { library } from "@fortawesome/fontawesome-svg-core" ;import { faPhone,faHeart,faHome } from "@fortawesome/free-solid-svg-icons" ;library.add(faPhone) library.add(faHeart) library.add(faHome)
简单封装
1 2 3 4 5 6 7 8 9 10 11 import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome" ;import * as fontIconModules from "@fortawesome/free-solid-svg-icons" ;import {useIcon} from "@/utils/fontAwesomeIconUtils" ;const iconObj = {...fontIconModules};for (const iconObjKey in iconObj) { if (Object .prototype.toString.call(iconObj[iconObjKey]) === "[object Object]" ) { library.add(iconObj[iconObjKey]) } }
Vue3+TS 其他资料