简书链接:vue组合式和选项的简单套路模板转换等笔记
文章字数:111,阅读全文大约需要1分钟
默写,默写很重要,看10编不如默写一遍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <scirpt> export default {
mounted(){ }, data(){ return { a:[], b:{}, c:"a"
} },methods:[ a(){ this.c=this.c+"aa"; console.log(this.c); }
] </script>
|
组合式
1 2 3 4 5 6 7 8 9
| <script setup> import {ref } from 'vue' const a=ref([]] const b=ref({}) const c=ref("a") function a(){ c.value=c.value+"aa"; } </script>
|
一些 bind 笔记本
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<script> import {ref,onMounted } from 'vue'
const x=ref("xxx") const options1=ref([]) const selects=ref([]) const checks=ref([])
const options=ref([ { "title":"你好", "value":"a" }, { "title":"你好1", "value":"b" }, { "title":"你好2", "value":"c" } ]) onMounted(()=>{})
</script> <template> <input v-model="x"/> {{x}} <p> current check: {{checks}} current options: {{options}} current options1: {{options1}} {{selects}} </p> <input type="checkbox" id="a" value="a1" v-model="checks"/> <select v-model="options1"> <option disabled dvalue="">please choose</option> <option>a</option> <option>b</option> <option>c</option> </select> <select v-model="selects"> <option disabled value="">please choose default</option> <option v-for="option in options" :value="option.value"> {{option.value}}</option> </select> </template>
|
更多对比
组合式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <script setup > import {ref,onMounted} from 'vue' onMounted(()=> { pElementRef.value.textContent = 'moun11111ted!' }) const a=ref("define a") const pElementRef=ref(null)
</script>
<template> {{a}} <p ref="pElementRef">hello</p> </template>
|
选项式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<script> export default { mounted() { this.$refs.pElementRef.textContent = 'mounted!' },data(){ return { a:"xx" } } } </script>
<template> {{a}} <p ref="pElementRef">hello</p> </template>
|
引用其他组件
这一次 组件式要少写一些代码
1 2 3 4 5 6 7 8 9 10
| import ChildComp from './ChildComp.vue'
export default { components: { ChildComp } } <template> <ChildComp /> </template>
|
1 2 3 4 5 6 7
| <script setup> import ChildComp from './ChildComp.vue' </script>
<template> <ChildComp /> </template>
|
props
1 2 3 4 5 6 7
| <!-- ChildComp.vue --> <script setup> const props = defineProps({ msg: String }) </script> <ChildComp :msg="greeting" />
|
1 2 3 4 5 6 7
| // 在子组件中 export default { props: { msg: String } } <ChildComp :msg="greeting" />
|
emit
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
| <script> import ChildComp from './ChildComp.vue'
export default { components: { ChildComp }, data() { return { childMsg: 'No child msg yet' } } } </script>
<template> <ChildComp @response="(msg) => childMsg = msg" /> <p>{{ childMsg }}</p> </template>
child <script> export default { emits: ['response'], created() { this.$emit('response', 'hello from child') } } </script>
<template> <h2>Child component</h2> </template>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <script setup> import { ref } from 'vue' import ChildComp from './ChildComp.vue'
const childMsg = ref('No child msg yet') </script>
<template> <ChildComp @response="(msg) => childMsg = msg" /> <p>{{ childMsg }}</p> </template> child
<script setup> const emit = defineEmits(['response'])
emit('response', 'hello from child') </script>
<template> <h2>Child component</h2> </template>
|