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 ...
随机推荐
- functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html
functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html
- 8.3 Customizing Git - Git Hooks 钩子 自动拉取 自动部署 提交工作流钩子,电子邮件工作流钩子和其他钩子
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks https://github.com/git/git/blob/master/temp ...
- gitignore 不起作用的解决办法 不再跟踪 让.gitignore生效,跟踪希望被跟踪的文件
实践 # https://git-scm.com/docs/gitignore https://git-scm.com/docs/gitignore 不跟踪log目录下的所有文件,但需要保留这个文件夹 ...
- Redis主从、哨兵模式的搭建
壹.Redis主从分离 准备三个redis配置文件(redis.conf),分别修改为redis6380.conf.redis6381.conf.redis6382.conf 一.配置Master 1 ...
- WPF mvvm 验证,耗时两天的解决方案
常用类 类名 介绍 ValidationRule 所有自定义验证规则的基类.提供了让用户定义验证规则的入口. ExceptionValidation 表示一个规则,该规则检查在绑定源属性更新过程中引发 ...
- 信息: TLD skipped. URI: http://java.sun.com/jstl/* is already defined解决方法
整合Spring MVC由于用到jstl,所以假如jstl便签用的jar包,启动tomcat时控制台出现了如下的输出: standard.jar与jstl.jar一起使用,但是jstl 1.2版本的就 ...
- Mysql 5.5升级5.8
前言,因为升级跳板机,需要将mariadb 升级到10.2,也就是对应MySQL的5.8,废话不多说下面开始进行mariadb 5.5 的升级 Welcome to the MariaDB monit ...
- OpenStack (cinder存储服务)
cinder简介 提供 OpenStack 存储块(Volume)服务,该管理模块原来也为 Nova 的一部分,即 Nova-volume,后来从 Folsom 版本开始使用 Cinder 来分离出块 ...
- Java|ArrayList源码分析|add()增加方法和grow()扩容方法
本文结构: 1.介绍特点 2.基本方法 3.重点源码分析 1.介绍特点 ArrayList: 是List的一个具体实现子类,是List接口的一个数组实现 (里面必定维护了一个数组). 默认初始容量10 ...
- 跟着Vimtutor学习Vim
跟着Vimtutor学习Vim Lesson 1 1.1 移动光标 在Vim中移动光标,分别使用h.j.k.l键代表左.下.上.右方向. 1.2 退出VIM :q! <ENTER> 退出V ...