vue & watch props
vue & watch props
bug

OK

watch: {
// props
// chatObj: () => {
// // bug
// log(`this.chatObj =`, JSON.stringify(this.chatObj, null, 4));
// },
chatObj: (newValue, oldValue) => {
// OK
log(`old chatObj =`, JSON.stringify(oldValue, null, 4));
log(`new chatObj =`, JSON.stringify(newValue, null, 4));
},
},
watch & args
new Vue({
el: '#app',
data: {
text: 'Hello'
},
components: {
'child' : {
template: `<p>{{ myprop }}</p>`,
props: ['myprop'],
watch: {
myprop: function(newVal, oldVal) { // watch it
console.log('Prop changed: ', newVal, ' | was: ', oldVal)
}
}
}
}
});

watch: {
// props
chatObj() {
// bug
log(`this.chatObj =`, JSON.stringify(this.chatObj, null, 4));
},
},
https://stackoverflow.com/questions/44584292/vuejs-2-0-how-to-listen-for-props-changes
https://forum.vuejs.org/t/cant-watch-component-props-as-object/28005
watch deep
deep: true,
const something = {
template: '<div><pre>{{ updateData }}</pre></div>',
props: {
updateData: Object,
},
watch: {
updateData: {
handler (val) {
console.log('watch', val.message);
},
deep: true,
},
},
};
https://codepen.io/xgqfrms/pen/MNmZNo

vue & watch & props
v-if & loading 异步加载数据,然后渲染组件
https://github.com/xgqfrms/vue/issues/86

demo
parent

child
<template>
<el-dialog
title="表单编辑"
class="form-modal-box"
:before-close="closeForm"
:visible.sync="visible">
<el-form
:rules="rules"
size="small"
:model="form">
<el-form-item
label="投放链接"
prop="putLink"
class="required-symbol"
:label-width="formLabelWidth">
<el-input
class="required-input"
v-model="form.putLink"
@input="inputChange"
@change="inputChange"
autocomplete="off">
</el-input>
</el-form-item>
<el-form-item
label="主标题"
class="required-symbol"
prop="title"
:label-width="formLabelWidth">
<el-input
class="required-input"
v-model="form.title"
autocomplete="off">
</el-input>
<el-alert
class="required-input"
v-if="titleLengthLimit"
:closable="false"
title="主标题: 长度 8个字以内"
type="warning">
</el-alert>
</el-form-item>
<el-form-item
label="副标题"
class="required-symbol"
prop="subTitle"
:label-width="formLabelWidth">
<el-input
class="required-input"
v-model="form.subTitle"
autocomplete="off">
</el-input>
<el-alert
class="required-input"
v-if="subTitleLengthLimit"
:closable="false"
title="副标题: 长度 13个字以内"
type="warning">
</el-alert>
</el-form-item>
<!-- <el-form-item
label="投放时间"
class="required-symbol"
required
:label-width="formLabelWidth">
<el-date-picker
style="width: 100%"
class="required-input"
@change="datePickerHandler"
v-model="form.putTime"
:clearable="false"
format="yyyy-MM-dd HH:mm:ss"
value-format="yyyy-MM-dd HH:mm:ss"
type="datetimerange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期">
</el-date-picker>
<el-alert
class="required-input"
v-if="dateEmpty"
:closable="false"
title="投放日期不可为空"
type="warning">
</el-alert>
</el-form-item> -->
</el-form>
<div
slot="footer"
class="dialog-footer">
<el-button
size="small"
@click="closeForm">
取消
</el-button>
<el-button
@click="saveForm"
size="small"
type="primary">
确定
</el-button>
</div>
</el-dialog>
</template>
<script>
const log = console.log;
const tomorrow = new Date().getTime() + 3600 * 24 * 1000;
export default {
name: "FormModal",
props: {
dialogFormData: {
type: Object,
default: () => {},
},
dialogFormVisible: {
type: Boolean,
default: () => false,
},
isAdd: {
type: Boolean,
default: () => false,
},
},
data() {
return {
form: {
putLink: "",
title: "",
subTitle: "",
putTime: ["", ""],
},
formLabelWidth: "80px",
visible: false,
rules: {
putLink: [
{
required: true,
message: '请输入投放链接',
trigger: ['blur', 'change'],
},
],
title: [
{
required: true,
message: '请输入主标题',
trigger: ['blur', 'change'],
},
// {
// min: 1,
// max: 8,
// message: '主标题长度在 8 个字以内',
// trigger: ['blur', 'change'],
// },
],
subTitle: [
{
required: true,
message: '请输入副标题',
trigger: ['blur', 'change'],
},
// {
// min: 1,
// max: 13,
// message: '副标题长度在 13 个字以内',
// trigger: ['blur', 'change'],
// },
],
startTime: [
{
type: 'date',
required: true,
message: '请输入投放时间范围',
trigger: ['blur', 'change'],
},
],
endTime: [
{
type: 'date',
required: true,
message: '请输入投放时间范围',
trigger: ['blur', 'change'],
},
],
putTime: [
{
type: 'datetimerange',
required: true,
message: '请输入投放时间范围',
trigger: ['blur', 'change'],
},
],
},
isValidated: true,
};
},
methods: {
inputChange(value = ``) {
this.form.putLink = value.trim();
},
closeForm() {
this.$emit(`close-put-item`);
},
timeStringToTimestamp(str) {
return new Date(str).getTime();
},
saveForm() {
let {
putId,
putLink,
title,
subTitle,
putTime,
} = this.form;
const flag = this.isAdd;
let data = {
putLink,
title,
subTitle,
// startTime: this.timeStringToTimestamp(putTime[0]),
// endTime: this.timeStringToTimestamp(putTime[1]),
};
if(!flag) {
data = {
id: putId,
...data,
};
}
if(putLink && title && subTitle && putTime[0]) {
this.$emit(`save-put-item`, data, flag);
} else {
this.$message({
type: 'error',
message: '必填字段不可为空!'
});
}
},
datePickerHandler(values = ``) {
// log(`date values`, values[0], values[1]);
this.form.putTime = [values[0], values[1]];
},
},
computed: {
titleLengthLimit() {
const {
title,
} = this.form;
return title.length > 8 ? true : false;
},
subTitleLengthLimit() {
const {
subTitle,
} = this.form;
return subTitle.length > 8 ? true : false;
},
dateEmpty() {
const {
putTime,
} = this.form;
return !putTime[0] ? true : false;
},
},
mounted() {
this.visible = this.dialogFormVisible;
},
watch: {
isAdd(newProp, oldProp) {
log(`\nthis.isAdd`, newProp, oldProp);
// this.isAdd = newProp;
},
dialogFormVisible(newProp, oldProp){
this.visible = newProp;
this.form = {
putLink: "",
title: "",
subTitle: "",
putTime: [moment().format('YYYY-MM-DD HH:mm:ss'), moment(tomorrow).format('YYYY-MM-DD HH:mm:ss')],
};
},
dialogFormData(newProp, oldProp){
if (this.isAdd) {
this.form = {
putLink: "",
title: "",
subTitle: "",
putTime: [moment().format('YYYY-MM-DD HH:mm:ss'), moment(tomorrow).format('YYYY-MM-DD HH:mm:ss')],
};
} else {
this.form = newProp;
}
},
},
beforeDestroy() {
log(`before destroy`);
},
destroyed() {
log(`destroyed`);
},
}
</script>
<style lang="less">
@import url("./form-modal.less");
</style>
modal

parent
<!-- modal -->
<FormModal
:dialogFormVisible="dialogFormVisible"
:isAdd="isAdd"
:dialogFormData="dialogFormData"
@close-put-item="closePutItem"
@save-put-item="savePutItem"
/>
methods: {
openPutItem() {
this.isAdd = true;
this.dialogFormData = this.initFormData;
this.dialogFormVisible = true;
},
closePutItem() {
this.dialogFormVisible = false;
},
savePutItem(data = {}, flag = false) {
this.closePutItem();
this.updateData(data, flag);
},
}
child
closeForm() {
this.$emit(`close-put-item`);
},
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
vue & watch props的更多相关文章
- [转]Vue中用props给data赋初始值遇到的问题解决
原文地址:https://segmentfault.com/a/1190000017149162 2018-11-28更:文章发布后因为存在理解错误,经@Kim09AI同学提醒后做了调整,在此深表感谢 ...
- Vue中用props给data赋初始值遇到的问题解决
Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14 作者:yuyongyu 我要评论 这篇文章主要介绍了Vue中用props给dat ...
- Vue computed props pass params
Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...
- vue & components & props & methods & callback
vue & components & props & methods & callback demo solution 1 & props & data ...
- vue & modal props & form data update bug
vue & modal props & form data update bug OK <div> <BindModal :dialogBindVisible=&qu ...
- vue之props父子组件之间的谈话
眨眼就来杭州两年了,时间真快. 我们今天来说说vue的一个api---->props 首先我们先看看一个例子,是我一个项目中写的. 看到这个:有木有一点懂了.要是没懂,继续往下看 这里我们用到了 ...
- vue的props和$attrs
过去我们在vue的父子组件传值的时候,我们先需要的子组件上用props注册一些属性: <template> <div> props:{{name}},{{age}} 或者 {{ ...
- vue学习--Props
Props: props用以从父组件接收数据: 使用: Vue.component('child',{ ...
- vue使用props动态传值给子组件里的函数用,每次更新,呼叫函数
父组件 <template> <div id="app"> <div>详情内容</div> <button v-on:clic ...
随机推荐
- REST 架构的替代方案 为什么说GraphQL是API的未来?
Managing enterprise accounts - GitHub Docs https://docs.github.com/en/graphql/guides/managing-enterp ...
- 通过Knockd隐藏SSH,让黑客看不见你的服务器
出品|MS08067实验室(www.ms08067.com) 本文作者:大方子(Ms08067实验室核心成员) 0X01设备信息 Ubuntu14.04:192.168.61.135 Kali ...
- 用werkzeug实现一个简单的python web框架
使用工具 Pycharm , Navicat , WebStorm等 使用库 Werkzeug用于实现框架的底层支撑,pymysql用于实现ORM,jinja2用于模板支持,json用于返回json数 ...
- .NET 微服务
前文传送门: 什么是云原生? 现代云原生设计理念 Microservices 微服务是构建现代应用程序的一种流行的体系结构样式,云原生系统拥抱微服务. 微服务是由一组(使用共享结构交互的.独立的小块服 ...
- P4254 [JSOI2008]Blue Mary开公司 (李超树)
题意:插入一些一次函数线段 每次询问在x = x0处这些线段的最大值 题解:李超树模版题 维护优势线段 注意这题的输入是x=1时的b #include <iostream> #includ ...
- Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)
题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...
- Gym - 102062A、B、C、D、E、F、G、H
比赛链接:https://vjudge.net/contest/409725#problem 题面点此处进入 Gym - 102062A 题意: 就是说比赛一共发a+b+c+d个牌子,现在不带上主人公 ...
- Trap HDU - 6569 二分
题意: 给你n个边长ai,你需要挑出来4个边使得它们可以构成等腰梯形.问你能构成多少种不同的等腰梯形 题解: 我们首先处理一下边长为x的且这个边长出现大于等于2次的边,因为等腰梯形需要两条相等的边 然 ...
- Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) C. Division (数学)
题意:有两个数\(p\)和\(q\),找到一个最大的数\(x\),使得\(p\ mod\ x=0\)并且\(x\ mod\ q\ne 0\). 题解:首先,如果\(p\ mod\ q\ne0\),那么 ...
- SQL Server的嵌套存储过程中使用同名的临时表怪像浅析
SQL Server的嵌套存储过程,外层存储过程和内层存储过程(被嵌套调用的存储过程)中可以存在相同名称的本地临时表吗?如果可以的话,那么有没有什么问题或限制呢? 在嵌套存储过程中,调用的是外层存 ...