每個人抱歉,我問了個愚蠢的問題,但我已經(jīng)試了很多了。我要做的是將getUser()函數(shù)的結果傳遞給我的Home.vue應用程序。但首先,我嘗試使用props傳遞簡單變量“counter”:
main.js
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
Vue.config.productionTip = false;
export default new Vue({
router,
store,
el: '#app',
props: { 'counter': 1 },
template: '<app v-bind:counter="counter" />',
components: { App },
created () {
// fetch the data when the view is created and the data is
// already being observed
this.getUser()
},
methods: {
getUser() {
fetch('/api/auth/user/user/')
.then(response => response.json())
.then(data => console.log(data));
}
},
});
Home.vue
<template lang="pug">
#app
.card(v-for="profile in profiles")
.card-header
button.btn.btn-clear.float-right(@click="deleteProfile(profile)")
.card-title {{ profile.user }}
.card-body {{ profile.phone_number }}
.card-body {{ profile.address }}
.card-body {{ counter }}
</template>
<script>
import { mapGetters } from 'vuex'
export default {
name: 'profile-list',
computed: mapGetters(['profiles']),
props: ['counter'],
methods: {
deleteProfile (profile) {
// Вызываем действие `deleteNote` из нашего хранилища, которое
// попытается удалить заметку из нашех базы данных, отправив запрос к API
this.$store.dispatch('deleteProfile', profile)
}
},
beforeMount () {
// Перед тем как загрузить страницу, нам нужно получить список всех
// имеющихся заметок. Для этого мы вызываем действие `getNotes` из
// нашего хранилища
this.$store.dispatch('getProfile')
},
}
我想做的是在profile.address下打印“counter”,但這不起作用。非常感謝。
我沒有看到任何計數(shù)器的初始化。只需將代碼從
props: { 'counter': 1 }
更改為了解更多:https://v3.vuejs.org/api/options-data.html#data