父组件
<demand :state='state' @stateValue='stateValue'></demand>
@Component({
components: {
demand: () => import('./demand/demand.vue'),
},
})
public state : boolean = false;//需求弹框
public stateValue(value : boolean) : void {
this.state = value;
}
子组件
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
@Component
export default class Demand extends Vue {
//这里跟直接用js写的有点区别,
//前者父组件传过来的值(这里是state)在子组件中直接拿来用就好,
//这里必须要赋给第三方变量,而且还要对父组件传过来的值(state)进行监听才可以成功。
//不然关闭弹框(子组件传给父组件)时候会报一个错误,大概意思就是说不能使用同一个变量??!!还有待学习
//反正现在使用watch监听可以成功操作
@Prop() public state : any; //子组件接收过来的值
public modal: any = false;//弹框显示的布尔值
//关闭弹框 并传给父组件
public close() : void {
this.$emit('stateValue',false);
}
}
//监听从父组件传过来的值,并赋给子组件的变量
@Watch('state') Modal(val: any): void {
this.modal = val;
}
}