簡體   English   中英

如何在 vue 中呈現 axios 響應上的組件?

[英]How to render component on axios response in vue?

當 axios 響應出現Add組件時,有組件Addindex我想渲染(實際上替換) index組件

添加組件

<template>
  <div>
    <v-container fluid>
      <v-form ref="form" @submit.prevent="submit" lazy-validation v-model="valid">
        <v-col cols="8">
          <v-card>
            <v-card-title>
              Add Project
              <v-spacer></v-spacer>
            </v-card-title>
            <v-card-text>
              <v-col cols="6">
                <v-text-field label="URL" class="mb-4" v-model="form.url"></v-text-field>
              </v-col>
              <v-col cols="6">
                <v-text-field label="Name" v-model="form.name"></v-text-field>
              </v-col>
              <v-col cols="6">
                <v-text-field label="Secret Key" v-model="form.secret"></v-text-field>
              </v-col>
              <v-btn type="submit" color="primary" class="ml-4">Save</v-btn>
            </v-card-text>
          </v-card>
        </v-col>
      </v-form>
    </v-container>
  </div>
</template>

<script>
import axios from "axios";
import { mapGetters } from "vuex";
import { api } from "~/config";
import Form from "~/mixins/form";

export default {
  mixins: [Form],
  data: () => ({
    passwordHidden: true,

    // label: {
    //   password: 'New Password',
    //   password_confirmation: 'Confirm New Password',
    // },

    form: {
      name: null,
      url: null,
    },
  }),

  methods: {
    submit() {
      if (this.$refs.form.validate()) {
        this.loading = true;

        axios
          .post(api.path("addProject"), this.form)
          .then((res) => {

            this.$toast.success('Your profile successfully updated.')
            console.log(res);
          })
          .catch((err) => {
            this.handleErrors(err.response.data.errors);
          })
          .then(() => {
            this.loading = false;
          });
      }
    },
  }
 
};
</script>

索引組件的路徑是

從 $comp/projects/Index.vue 導入索引

當 axios 響應出現Add組件時,有組件Addindex我想渲染(實際上替換) index組件

添加組件

<template>
  <div>
    <v-container fluid>
      <v-form ref="form" @submit.prevent="submit" lazy-validation v-model="valid">
        <v-col cols="8">
          <v-card>
            <v-card-title>
              Add Project
              <v-spacer></v-spacer>
            </v-card-title>
            <v-card-text>
              <v-col cols="6">
                <v-text-field label="URL" class="mb-4" v-model="form.url"></v-text-field>
              </v-col>
              <v-col cols="6">
                <v-text-field label="Name" v-model="form.name"></v-text-field>
              </v-col>
              <v-col cols="6">
                <v-text-field label="Secret Key" v-model="form.secret"></v-text-field>
              </v-col>
              <v-btn type="submit" color="primary" class="ml-4">Save</v-btn>
            </v-card-text>
          </v-card>
        </v-col>
      </v-form>
    </v-container>
  </div>
</template>

<script>
import axios from "axios";
import { mapGetters } from "vuex";
import { api } from "~/config";
import Form from "~/mixins/form";

export default {
  mixins: [Form],
  data: () => ({
    passwordHidden: true,

    // label: {
    //   password: 'New Password',
    //   password_confirmation: 'Confirm New Password',
    // },

    form: {
      name: null,
      url: null,
    },
  }),

  methods: {
    submit() {
      if (this.$refs.form.validate()) {
        this.loading = true;

        axios
          .post(api.path("addProject"), this.form)
          .then((res) => {

            this.$toast.success('Your profile successfully updated.')
            console.log(res);
          })
          .catch((err) => {
            this.handleErrors(err.response.data.errors);
          })
          .then(() => {
            this.loading = false;
          });
      }
    },
  }
 
};
</script>

索引組件的路徑是

從 $comp/projects/Index.vue 導入索引

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM