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. __new__() to create it, and __init__() to customize it 类方法 实例方法

    https://docs.python.org/3/reference/datamodel.html#object.__init__

  2. 7. A typical stream socket session

    http://publibfp.dhe.ibm.com/epubs/pdf/f1a2d400.pdf Read and write data on socket s, using the send() ...

  3. 用tqdm和rich为固定路径和目标的python算法代码实现进度条

    适用场景 在存在固定长度的算法中可以可视化算法执行的过程,比如对一个固定长度的数组的遍历,就是一种适合使用进度条来进行可视化的场景.而一些条件循环,比如while循环,不一定适合使用进度条来对算法执行 ...

  4. MySQL的索引为什么用B+Tree?InnDB的数据存储文件和MyISAM的有何不同?

    前言 这篇文章的题目,是我真实在面试过程中遇到的问题,某互联网众筹公司在考察面试者MySQL相关知识的第一个问题,我当时还是比较懵的,没想到这年轻人不讲武德,不按套路出牌,一般的问MySQL的相关知识 ...

  5. http状态码、错误分析

    客户端的每一次请求,服务器都必须给出回应.回应包括 HTTP 状态码和数据两部分. HTTP状态码五大类: 状态码 响应类别 出现原因 1XX  信息性状态码(Informational) 服务器正在 ...

  6. python join()方法的使用,可以应用到tcp压测发送指定数据长度的应用

    Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串.其中,序列中的元素应是字符串类型. 学习join()方法主要是为了配合随机数的使用,生产某个指定位数的随机数,在t ...

  7. 向指定url发送Get/Post请求

    向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 2.HttpUtil 工具类–向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 impor ...

  8. MapReduce编程练习(四),统计多个输入文件学生的平均成绩,

    问题描述: 在输入文件中,有多个,其中每个输入文件代表一个学生的各科成绩,其中每行的数据形式为<科目,成绩>,你需要将每个文件中的每科目的成绩进行统计,然后求平均值. 输入文件格式: 这里 ...

  9. 在 .NET Core Logging中使用 Trace和TraceSource

    本文介绍了在.NET Core中如何在组件设计中使用Trace和TraceSource. 在以下方面会提供一些帮助: 1.你已经为.NET Framework和.NET Core / .NET Sta ...

  10. 给jekyll博客添加搜索功能

    使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...