本帖最后由 ing 于 2020-7-6 08:35 编辑
问题来源:主要是 watch 监听的是对象导致了这一问题,还有 在这个组件上使用 v-model
————————————————————————————————————————————————————————————————————
最奇怪的是生命周期函数 created 被提示“从未使用”
[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<base-input v-bind:user="user"></base-input>
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
user:{
name:'',
firstName:'test',
lastName:''
}
},
watch:{
user:function () {//一旦user内容发生改变这个函数应该被立刻调用(实际没有)
this.debouncedGetName();
console.log("user:function ()");
}
},
created:function() {
this.debouncedGetName = _.debounce(this.getName,600)
},
methods:{
getName:function(){
let lastname = new Array();
this.user.firstName = this.user.name.split(' ')[0];
if (this.user.name.split(' ').length > 1)
for (let i = 1; i<this.user.name.split(' ').length; i++)
lastname[i] = this.user.name.split(' ')[i];
this.user.lastName = lastname.join('');
console.log("getName:function ()");
}
},
components:{
"base-input":{
props:['user'],
template:`
<div>
<input type="text" v-model="user.name">
<slot>
<p>FitstName:{{user.firstName}}</p>
<p>LastName:{{user.lastName}}</p>
</slot>
</div>
`
}
}
});
</script>
</body>
</html> |