父向子 v-bind propsvue
<!-- 组件使用v-bind传值 -->
<router :msg="msg"></router>
子组件: <p>子组件 ----- {{msg}}</p> props: ["msg"], //props接收
props:验证 vuex
props: { // fooA只接受数值类型的参数 fooA: Number, // fooB能够接受字符串和数值类型的参数 fooB: [String, Number], // fooC能够接受字符串类型的参数,而且这个参数必须传入 msg: { type: String, required: true }, // fooD接受数值类型的参数,若是不传入的话默认就是100 fooD: { type: Number, default: 100 }, // fooE接受对象类型的参数 fooE: { type: Object, // 当为对象类型设置默认值时必须使用函数返回 default: function() { return { message: "Hello, world" }; } }, // fooF使用一个自定义的验证器 fooF: { validator: function(value) { return value >= 0 && value <= 100; } },
fooG: {
type:Array,
// 当为数组类型设置默认值时必须使用数组返回 default: function() {
return [];
}
},
}
props 是单向绑定的:当父组件的属性变化时,将传导给子组件,可是不会反过来。这是为了防止子组件修改父组件的状态。因此不该该在子组件中修改 props 中的值,Vue 警告。api
这是我上次想修改父组件的值遇到的报错:数组
子向父 v-on $emit函数
子组件: <button @click="cyy">按钮</button> methods: { cyy() { this.$emit("zifu", "子组件向父组件传值", true); } } 父组件: <router v-on:zifu="hehe"></router> methods: { hehe: function(data, data2) { console.log(data, data2); } }

<div> //爸爸A <router></router> //哥哥A1 <vuex></vuex> //弟弟A2 </div>
A1要向A2传值 、 能够用$emit传给A、A在使用v-bind传给A2 ui
使用父组件作中转 这里不举例了只是把上面的子向父,父向子连起来用this
<div> //爸爸A <router></router> //哥哥A1 <vuex></vuex> //弟弟A2 </div>
哥哥A1组件:spa
<button @click="cyy">按钮</button> 点击按钮向弟弟A2传值
脚本中:
import Bus from "../api/Bus"; //注意引入 export default { data() { return { a: 1 }; }, methods: { cyy() { Bus.$emit("zifu", this.a++, "子组件向兄弟组件传值"); //存 Bus.$emit } } };
弟弟A2组件:code
<p>接受兄弟A1传值=-------第{{ccc}}次,向{{ddd}}</p>
脚本中:
import Bus from "../api/Bus"; export default { data() { return { ccc: "", ddd: "" }; }, created() { Bus.$on("zifu", (val, val1) => { //取 Bus.$on this.ccc = val; this.ddd = val1; }); } };

传值就告一段落了~~~~~~~~~~~~by~~~router