场景:父组件向子组件传递数据,子组件去试图改变父组件数据的时候。

解决:子组件通过事件向父组件传递信息,让父组件来完成数据的更改。

比如:我的父组件是普通页面,子组件是弹窗的登录界面,父组件传递的数据(reloginDialog)控制登录界面的显示(reloginDialog = true),当登陆成功后,子组件触发一个事件,父组件捕获后(reloginDialog = false)将登录界面隐藏。

父组件调用

<re-login :dialogProp="reloginDialog" @login-success="reloginDialog = $event"></re-login>

子组件详细【ReLogin.vue】

<template>
<v-layout row justify-center>
<v-dialog v-model="dialogProp" persistent max-width="500">
<v-card class="login-bg">
<v-card-text>
<v-container fluid fill-height>
<v-layout align-center justify-center row fill-height text-xs-center>
<v-flex xs12>
<v-card color="#ffffffe6">
<v-card-title></v-card-title>
<h1 class="headline text-xs-center">****管理系统</h1>
<v-form
ref="form"
lazy-validation
>
<v-flex class="xs8 offset-xs2">
<v-text-field
color="blue"
v-model="submitData.empCode"
label="用户名"
:rules="[rules.required]"
prepend-inner-icon="fas fa-user"
></v-text-field>
</v-flex>
<v-flex class="xs8 offset-xs2">
<v-text-field
color="blue"
v-model="submitData.password"
prepend-inner-icon="fas fa-lock"
:append-icon="show1 ? 'fas fa-eye' : 'fas fa-eye-slash'"
:rules="[rules.required, rules.min]"
:type="show1 ? 'text' : 'password'"
label="密码"
counter
@click:append="show1 = !show1"
></v-text-field>
</v-flex>
</v-form>
<v-flex class="xs8 offset-xs2">
<v-btn color="blue" block dark @click="login">登录</v-btn>
</v-flex>
<v-card-title></v-card-title>
</v-card>
<!--页面提示-->
<v-snackbar
v-model="snackbar.show"
multi-line
top
right
:timeout="snackbar.timeout"
color="blue-grey darken-4"
>
{{ snackbar.text }}
<v-btn
color="pink"
flat
fab
dark
@click="snackbar.show = false"
>
<v-icon>fas fa-times</v-icon>
</v-btn>
</v-snackbar>
</v-flex>
</v-layout>
</v-container>
</v-card-text>
</v-card>
</v-dialog>
</v-layout>
</template> <script>
export default {
name: "ReLogin",
props: ['dialogProp'],
data: () => ({
// 全局提示
snackbar: {
show: false,
timeout: 6000,
text: ''
},
show1: false,
submitData:{
empCode: '',
password:''
},
rules: {
required: value => !!value || '请填写此字段.',
min: v => v.length >= 6 || '最少6个字符'
}
}),
methods: {
login: function () {
let me = this;
let tip = this.snackbar;
let store = this.$store;
if (!this.$refs.form.validate()) {
tip.show = true;
tip.text = '请检查字段是否符合规则';
return;
}
store.state.axios.post('/admin/login', this.submitData)
.then(function (response) {
let data = response.data;
if (data.code == 0){
sessionStorage.setItem('LOGIN_TOKEN',JSON.stringify(data.data));
me.$emit('login-success',false);
me.submitData = {
empCode: '',
password:''
};
} else{
tip.show = true;
tip.text = data.msg;
}
})
.catch(function (error) {
console.log(error);
tip.show = true;
tip.text = error;
}); }
}
}
</script> <style scoped>
.login-bg{
/*background-image: url("../assets/bg.jpg");*/
background-image: url("../assets/bg-2.jpeg");
background-position: center;
background-size: cover;
}
</style>

关键在于这句话,触发父组件的事件

me.$emit('login-success',false);

当用户身份过期需要登录的时候

登陆成功之后隐藏登录面板,继续之前没完成的操作

Vue 报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders的更多相关文章

  1. Vue报错之"[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead......"

    一.报错截图 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the p ...

  2. vue报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

  3. [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

  4. 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop bei

    项目中遇到父组件传值 activeIndex <Tabs :tabs="tabs" :activeIndex="activeIndex" >< ...

  5. Avoid mutating a prop directly since the value will be overwritten whenever the parent component re

    子组件修改父组件的值踩坑 Vue1.0升级至2.0之后,直接在子组件修改父组件的值是会报错的 目的是为了阻止子组件影响父组件的数据. 我们都知道在vue中,父组件传入子组件的变量是存放在props属性 ...

  6. 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren

    今天在做Vue的时候,子组件关闭的时候,报如下错误 报错:vue.esm.js?65d7:610 [Vue warn]: Avoid mutating a prop directly since th ...

  7. Vue avoid mutating a prop directly since the value will be overwritten

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. (复习)父子组件传值使用v-modal双向绑定,报错Avoid mutating a prop directly解决方案

    报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent component. ...

  9. Vue 错误:Avoid mutating a prop directly

    Avoid mutating a prop directly since the value will be overwritten whenever the parent component re- ...

随机推荐

  1. 编程语言 Node.js中使用到的npm工具

    啥是npm? npm就是(node package manager)包结点管理器,它随同Node.js一起安装的,由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了. 同样可以通过输 ...

  2. 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props”

    未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props” ...

  3. typescript的函数

    1:默认参数(传入值会覆盖默认参数,不传值也行) function getinfo(name:string,age:number=20):string{ return `${name}---${age ...

  4. 基于LinedHashMap 实现LRUCache 缓存

    原文链接 基于LinedHashMap 实现LRUCache 缓存 基于LinkedHashMap实现LRUCache public class LRUCache2<K, V> exten ...

  5. TestLink-Windows安装教程

    TestLink-Windows安装教程 QQ群交流:585499566 一.这篇文章的目的 以后工作中要使用Testlink来管理测试的流程,需要在本地或者Testlink服务器上练习使用,在个人本 ...

  6. ubuntu18.04 ssh 远程系统拒绝连接 解决方法

    错误提示是这个: The remote system refused the connection. 原因是 Ubuntu 没安装  一个软件, 废话不多说 ,上解决方法: 执行该条命令,安装 ,安装 ...

  7. Java基础之入门

    写写基础,顺便回顾下,再深层次思考下哪些深入的没弄明白. Java是Sun Microsystems于1995年推出的高级编程语言  其版本 由 1.1 -> 1.2 -> 1.3 -&g ...

  8. android 6.0 Intent 安装apk闪退

    需求描述: 利用android系统自带的DownloadManager下载apk文件,并且打开安装界面. 问题描述: 关于DownloadManager的使用网上有很多例子,在此不啰嗦.下载完成之后在 ...

  9. 对ES6的yield示例分析

    近期ES6标准如火如荼的发展,其中主要还是各大浏览器的支持,最重要厂商支付宝,微信的支持,使得国内的发展也很迅猛. 这里主要是对yield关键字的,yield实际上可以看作是一种新的中断机制,大家都知 ...

  10. 正则表达式,提取html标签的属性值

    /** * 提取HTML标签的属性值 * @param source HTML标签内容 * "<a title=中国体育报 href=''>aaa</a><a ...