Nic*_*ngs 6 typescript vue.js vue-component vuejs2
我有一个简单的测试组件,模板如下所示:
<template>
<div>
<input type="text" v-model="name" class="form-control">
<h5>{{ message }}</h5>
</div>
</template>
<script src="./test.ts" lang="ts"></script>
Run Code Online (Sandbox Code Playgroud)
组件 TypeScript 如下所示:
declare var Vue: typeof Function;
declare var VueClassComponent: any;
import { Component, Inject, Model, Prop, Watch } from "vue-property-decorator";
@VueClassComponent.default({
template: require("./test.vue"),
style: require("./test.sass"),
props: {
name: String,
num: Number
}
})
export default class TestComponent extends Vue {
name: string;
num: number;
message: string = "";
@Watch("name")
protected onNameChanged(newName: string, oldName: string): any {
console.log("setting " + oldName + " to " + newName);
}
mounted(this: any): void {
console.log("mounted called");
this.message = "Hello " + this.name + " " + this.num;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在框中输入时input
,@Watch("name") 处理程序永远不会触发,但是我确实在以下位置收到这些错误console
:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "name"
Run Code Online (Sandbox Code Playgroud)
对于框中输入的每个字符一次input
。我不知道该名称在哪里设置,因为我没有在任何地方设置它。虽然这是我的目标(更新名称),但我一直在阅读您不能直接更改值,您需要设置 @Watch 处理程序,然后在其他地方设置它们(我仍然不明白具体如何设置,但可以现在甚至还没有得到它。
根据我们的讨论,这里问题的根源在于声明name
为属性。其目的是这name
是一个内部值,可以简单地用于导出message
。既然如此,手表就没有必要了,手表就可以了。
declare var Vue: typeof Function;
declare var VueClassComponent: any;
import { Component, Inject, Model, Prop, Watch } from "vue-property-decorator";
@VueClassComponent.default({
template: require("./test.vue"),
style: require("./test.sass"),
props: {
num: Number
}
})
export default class TestComponent extends Vue {
name: string;
num: number;
get message(){
return "Hello " + this.name + " " + this.num;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8820 次 |
最近记录: |