<fzzz></fzzz>;
import yyy from "./components/xxx.vue";
Vue.component("zzz", yyy);
-
// main.js
import inputSearch from "./components/inputSearch.vue";
Vue.component("inputSearch", inputSearch); // 3 种, i 开头
Vue.component("input-search", inputSearch); //
Vue.component("input-Search", inputSearch); //
Vue.component("InputSearch", inputSearch); // 6 种
Vue.component("Input-search", inputSearch); //
Vue.component("Input-Search", inputSearch); //
// main.js
import InputSearch from "./components/inputSearch.vue";
Vue.component("inputSearch", InputSearch); // 3 种, i 开头
Vue.component("input-search", InputSearch); //
Vue.component("input-Search", InputSearch); //
Vue.component("InputSearch", InputSearch); // 6 种
Vue.component("Input-search", InputSearch); //
Vue.component("Input-Search", InputSearch); //
建议使用大驼峰,因为这提高了模板的可读性,能帮助我们区分 Vue 组件和原生 HTML 元素。
<fyyy></fyyy>;
import yyy from "@/components/xxx.vue";
components: {
yyy;
}
-
<inputSearch></inputSearch>
<input-search></input-search>
<input-Search></input-Search>
import inputSearch from "@/components/inputSearch.vue"
import inputSearch from "@/components/input-search.vue"
import inputSearch from "@/components/input-Search.vue"
import inputSearch from "@/components/InputSearch.vue"
import inputSearch from "@/components/Input-search.vue"
import inputSearch from "@/components/Input-Search.vue"
components: { inputSearch }
<InputSearch></InputSearch>
<Input-search></Input-search>
<Input-Search></Input-Search>
<inputSearch></inputSearch>
<input-search></input-search>
<input-Search></input-Search>
import InputSearch from "@/components/inputSearch.vue"
import InputSearch from "@/components/input-search.vue"
import InputSearch from "@/components/input-Search.vue"
import InputSearch from "@/components/InputSearch.vue"
import InputSearch from "@/components/Input-search.vue"
import InputSearch from "@/components/Input-Search.vue"
components: { InputSearch }
child.vue
import child from '@/components/child.vue';
components: { child },
child.vue
import child from '@/components/child.vue';
child.vue
组件放在项目的 components 文件夹下,符合 components/组件名称/组件名称.vue 的目录结构,可直接引用
// 子组件
<view>{{ name }}</view>
// data()
data() {
return {
name: 'xxx'
}
},
// 父组件
<child></child>
// 子组件
<view>{{ name }}</view>
// props
props: {
name: {
type: String,
default: ''
},
},
// 父组件
<child name="xxx"></child>
// 子组件
<view @click="getValue">{{ name }}</view>
// props
props: {
name: {
type: String,
default: ''
},
},
methods:{
getValue() {
this.name = "yyy"; // 报错
}
}
// 父组件
<child name="xxx"></child>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PePniYhx-1662716305299)(app_files/2.jpg)]
// 子组件
<view @click="getValue">{{ name }}</view>
// data()、methods
data() {
return {
name: 'xxx'
}
},
methods: {
getValue() {
this.$emit('change', this.name)
}
}
// 父组件
<child @change="getName"></child>
// methods
methods: {
getName(value) {
console.log(value);
}
}
// 子组件
<view @click="getValue">{{ name }}</view>
//
props: {
undataName: {
type: String,
default: ''
},
},
data() {
return {
name: ''
}
},
methods: {
getValue() {
this.$emit('change', this.name)
this.name = this.undataName;
}
}
// 父组件
<child @change="getName" undataName="yyy"></child>
// methods
methods: {
getName(value) {
console.log(value);
}
}
Vue 父子组件如何双向绑定传值
Vue 官网 表单输入绑定
相当于
text = event.target.value">
<template>
<input :value="name" @input="onInput" />
template>
<script>
export default {
model: {
prop: "name", //这个字段,是指父组件设置 v-model 时,将变量值传给子组件的 name
event: "getName", //这个字段,是指父组件监听 getName 事件
},
props: {
name: {
type: String,
default: "",
},
},
methods: {
onInput(e) {
this.$emit("getName", e.target.value);
console.log(e.target.value);
},
},
};
script>
// 父组件
<template>
<view>
<child v-model="name">child>
view>
template>
<script>
import child from "@/components/child.vue";
export default {
components: {
child,
},
data() {
return {
name: "xxx",
};
},
watch: {
name(value) {
console.log(value, "--value");
},
},
};
script>