我正在使用Vue为CRUD应用程序创建alert 组件.js.我希望在保存数据后将消息传递给另一个组件.目前,我正试图在$router.push中传递这些数据,比如this.$router.push({path: '/', query: {alert: 'Customer Added'}}),然后在另一个组件中访问这些数据.然而,这并不像预期的那样工作,而是将数据传递到url中.

这是保存数据的组件Add.vue

<template>
<div class="add container">
<Alert v-if="alert" v-bind:message="alert" />
<h1 class="page-header">Add Customer</h1>
<form v-on:submit="addCustomer">
    <div class="well">
        <h4>Customer Info</h4>
        <div class="form-group">
            <label>First Name</label>
            <input type="text" class="form-control" placeholder="First Name" 
            v-model="customer.first_name">
        </div>
        <div class="form-group">
            <label>Last Name</label>
            <input type="text" class="form-control" placeholder="Last Name" 
            v-model="customer.last_name">
        </div>
    </div>
    <div class="well">
        <h4>Customer Contact</h4>
        <div class="form-group">
            <label>Email</label>
            <input type="text" class="form-control" placeholder="Email" v-model="customer.email">
        </div>
        <div class="form-group">
            <label>Phone</label>
            <input type="text" class="form-control" placeholder="Phone" v-model="customer.phone">
        </div>
    </div>

    <div class="well">
        <h4>Customer Location</h4>
        <div class="form-group">
            <label>Address</label>
            <input type="text" class="form-control" placeholder="Address" v-model="customer.address">
        </div>
        <div class="form-group">
            <label>City</label>
            <input type="text" class="form-control" placeholder="City" v-model="customer.city">
        </div>
        <div class="form-group">
            <label>State</label>
            <input type="text" class="form-control" placeholder="State" v-model="customer.state">
        </div>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</template>

<script>
import Alert from './Alert'
export default {
name: 'add',
data () {
    return {
    customer: {},
    alert:''
    }
},
methods: {
    addCustomer(e){
        if(!this.customer.first_name || !this.customer.last_name || 
!this.customer.email){
            this.alert = 'Please fill in all required fields';
        } else {
            let newCustomer = {
                first_name: this.customer.first_name,
                last_name: this.customer.last_name,
                phone: this.customer.phone,
                email: this.customer.email,
                address: this.customer.address,
                city: this.customer.city,
                state: this.customer.state
            }
            this.$http.post('http://slimapp.dev/api/customer/add', 
            newCustomer)
                .then(function(response){
                    this.$router.push({path: '/', query: {alert: 'Customer 
            Added'}})

                });
            e.preventDefault();
            }
            e.preventDefault();
            }
            },
            components: {
             Alert
            }
            }
            </script>

            <!-- Add "scoped" attribute to limit CSS to this component only 
            -->
            <style scoped>
            </style>

这是alert 组件,alert .vue

<template>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
{{message}}
</div>
</template>

<script>
export default {
name: 'alert',
props: ['message'],
data () {
return {

}
}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

这是用来查看alert 的组件,客户.vue

<template>
<div class="customers container">
<Alert v-if="alert" v-bind:message="alert" />
<h1 class="page-header">Manage Customers</h1>
<table class="table table-striped">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="customer in customers">
      <td>{{customer.first_name}}</td>
      <td>{{customer.last_name}}</td>
      <td>{{customer.email}}</td>
      <td></td></tr>
  </tbody>
</table>

</div>
</template>

<script>
import Alert from './Alert';

export default {
name: 'customers',
data () {
return {

  customers: [],
  alert: ''

 }
},

methods: {
fetchCustomers(){
  this.$http.get('http://slimapp.dev/api/customers')
    .then(function(response){

      this.customers = (response.body); 
    });
  }
 },
created: function(){
 if (this.$route.params.alert) {
   this.alert = $route.params.alert
 }
 this.fetchCustomers();
},
updated: function(){
this.fetchCustomers();
},
components: {
  Alert
  }
}

我该如何解决这个问题?

推荐答案

无法按您希望的方式通过vue路由传递数据.您只能传递如下参数:

Route definition:

{ path: '/products/:id/edit', name: 'products.edit', component: ProductForm },

然后你可以得到this.$route.params.id的参数

或者你可以:

this.$router.push({name: 'products.index', params: { id: 1 }})

我建议您添加一个GET参数,比如?success=true,或者在推送新路由之前显示一个带有100的alert .

Vue.js相关问答推荐

如何使用Nuxt 3动态处理无限嵌套路由?

Vuetify/Vue3 插槽模板之间的阴影

保持页面重新加载之间的状态

更新 Shopware 管理 CMS 组件中的编辑器视图

视图中的 vue 样式不适用于 html

VueJS 中的 VisJS 时间轴:带有图像的项目放错了位置

通过元素加中的正则表达式输入电话号码格式

Vue3 动态渲染

如何解决 Vue.js 中的相邻 JSX 元素必须包含在封闭标记中问题

如何在 vuejs 自定义指令中传递动态参数

如何在 Vue.js 中使用 MaterializeCss?

使用 aria-live 在 vue.js 中向屏幕阅读器宣布信息

如何根据生产或开发模式读取不同的 .env 文件?

将 Vuetify 从 1.5 升级到 2 时,Sass-loader 错误 'options has an unknown property 'indentedSyntax''

删除一行后刷新 Bootstrap-Vue 表

用于本地编辑的 Vuex 克隆对象

Vuetify RTL 风格

Vue.JS 2.0 修改不相关数据时速度慢

检测 Vue-Router 导航Guards中的后退按钮

在鼠标悬停时动态添加和删除类