上面的例子我们的组件里面data的属性是固定的,那么我有什么办法可以修改firstname 和 secondname的值呢?组件一般不能写的太死,组件的内容一般需要外边传
所以我们需要定义一些属性
也就是说我在使用组件的时候怎么传递变量给组件,并被组件接收呢?
1.组件中设置props用来接受外部变量
2.使用组件的时候,传入变量
<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body><script type="text/javascript" src="https://unpkg.com/vue/dist/vue.js"></script><div id="app"></div><script>//1.创建一个组件var MyCompose = {props:["firstname","secondname"],data() {return{title : "hello",}},//method和computed都可以保留methods: {fullname() {return this.firstname + this.secondname}},template: `<h3> {{ firstname }} {{ secondname }} {{ fullname() }} </h3>`,};//2.全局注册组件,注意最好使用驼峰写法// Vue.component("MyComp",MyCompose);//3.使用组件var vm = new Vue({el:"#app",//4.局部注册组件components: {MyCompose,},template:`<MyCompose firstname="a" secondname="b" ></MyCompose>`,})</script></body></html>
