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的更多相关文章

  1. [转]Vue中用props给data赋初始值遇到的问题解决

    原文地址:https://segmentfault.com/a/1190000017149162 2018-11-28更:文章发布后因为存在理解错误,经@Kim09AI同学提醒后做了调整,在此深表感谢 ...

  2. Vue中用props给data赋初始值遇到的问题解决

    Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14   作者:yuyongyu    我要评论   这篇文章主要介绍了Vue中用props给dat ...

  3. Vue computed props pass params

    Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...

  4. vue & components & props & methods & callback

    vue & components & props & methods & callback demo solution 1 & props & data ...

  5. vue & modal props & form data update bug

    vue & modal props & form data update bug OK <div> <BindModal :dialogBindVisible=&qu ...

  6. vue之props父子组件之间的谈话

    眨眼就来杭州两年了,时间真快. 我们今天来说说vue的一个api---->props 首先我们先看看一个例子,是我一个项目中写的. 看到这个:有木有一点懂了.要是没懂,继续往下看 这里我们用到了 ...

  7. vue的props和$attrs

    过去我们在vue的父子组件传值的时候,我们先需要的子组件上用props注册一些属性: <template> <div> props:{{name}},{{age}} 或者 {{ ...

  8. vue学习--Props

    Props:        props用以从父组件接收数据:                     使用:                Vue.component('child',{        ...

  9. vue使用props动态传值给子组件里的函数用,每次更新,呼叫函数

    父组件 <template> <div id="app"> <div>详情内容</div> <button v-on:clic ...

随机推荐

  1. 成为一名优秀的Java程序员9+难以置信的公式

    成为一名优秀的Java程序员 成为一名优秀的Java程序员并不重要,但是首先您应该了解基本的编程语言. 好吧,你知道那太好了.我们应该一步一步地精通Java编程,并应遵循所有说明,改进Java的编程逻 ...

  2. mysql中int型的数字怎么转换成字符串

    字段:number  是integer类型    在表test中 select cast(number as char) as number from test; 或者convert()方法.因为转换 ...

  3. 设计模式c++(1)

    本来是想把之前的<head first设计模式>看了,不过因为这本书是java实现的,跟c++还是略有区别. 于是找了一下,发现了一个不错的blog,打算连书带blog一起参考着看了. b ...

  4. java 对象之间的复制

    package com.jy.demo.web; import java.util.Date; public class People { private String name;//姓名 priva ...

  5. Java 复习整理day04

    在我们的日常生活中,方法可以理解为要做某件事情, 而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临 着一件事情(坐车到学校这件事情)需要解决,解决办法 呢?可采用坐公交车或坐出租车的 ...

  6. DEDECMS:解决BMP、jpeg图片或MP4视频无法上传和在后台无法显示

    一.BMP图片无法上传解决方法: 1.修改配置文件: 在include-->dialog的文件夹下, select_images_post.php--> 把 $sparr = Array( ...

  7. hdu4348 To the moon (主席树 || 离线线段树)

    Problem Description Background To The Moon is a independent game released in November 2011, it is a ...

  8. nuoyanli 520 Let‘s play computer game

    H题 描述 xxxxxxxxx在疫情期间迷上了一款游戏,这个游戏一共有nnn个地点(编号为1--n1--n1--n),他每次从一个地点移动到另外一个地点需要消耗 一定的能量,每一个地点都有一些珠宝,输 ...

  9. Codeforces Round #666 (Div. 2) C. Multiples of Length (贪心)

    题意:给你一个由\(0,1,?\)组成的字符串,你可以将\(?\)任意改成\(0\)或\(1\),问你操作后能否使得该字符串的任意长度为\(k\)的区间中的\(0\)和$1的个数相等. 题解:我们首先 ...

  10. Linux-平均负载指数

    目录 系统平均负载 什么是平均负载 平均负载多少合理 如何观察平均负载 平均负载和CPU的使用率的区别 平均负载分析 执行CPU密集型任务 执行I/O密集型任务 大量进程调度 关于平均负载的总结 系统 ...