0
我正在使用vue-resource在資源上執行CRUD,例如,文章標籤。對於CUD,我需要發送一個csrf令牌以及數據。我的服務器端代碼接受一個自定義HTTP頭'X-CSRF-TOKEN'並檢查它的值。參考/在同一Vue組件的其他選項中使用屬性
我爲每個標籤條目製作標籤組件。該CSRF令牌來自root用戶實例,並綁定到標籤組件,就像這樣:
<tr v-for="tag in tags" is="tag" :tag="tag" :token="token"></tr>
,這裏是模板
<template id="tag-template">
<tr>
<td>@{{ tag.id }}</td>
<td>@{{ tag.name }}</td>
<td>
<button type="button" class="btn btn-xs btn-outline green">Edit</button>
<button type="button" class="btn btn-xs btn-outline red" @click="deleteTag(tag,token)">Delete</button>
</td>
</tr>
</template>
據VUE資源文檔,我可以設置自定義標題是這樣:Vue.http.headers.common ['X-CSRF-TOKEN'] =標記;我也可以在組件設置中預設此標題。
下面的代碼是我想達到什麼:
Vue.component('tag', {
props: ['tag', 'token'],
template: '#tag-template',
methods: {
deleteTag: function (tag, token) {
// I don't want to repeat this line in my updateTag(), createTag() etc.
Vue.http.headers.common['X-CSRF-TOKEN'] = token;
this.$http.delete('/api/tags/' + tag.id).then(
function() {
vm.tags.$remove(tag);
},
function() {}
);
}
},
http: {
headers: {
// how to reference/use prop 'token' here?
'X-CSRF-TOKEN': 'token from prop list'
}
}
});
的問題是,我怎麼可能會在其他組件選項使用道具?
謝謝。
那麼如何在我的組件中引用根實例的數據字段,特別是在其他選項對象中?我嘗試了一些像'X-CSRF-TOKEN':vm.token',但沒有運氣。 (我將根實例分配給一個變量虛擬機。) – Carter
如上所述,做全局變量 – gurghet