我在下面有這個例子,我使用下拉串列來過濾資料。如您所見,我將 [items] 硬編碼到 javascript 檔案中。我希望指向一個外部 javascript 檔案,并希望有人可以幫助我以最好的方式做到這一點:
data: {
selectedType: '',
selectedCountry: '',
selectedYear: '',
items: [{
name: 'Nolan',
type: 'mercedes, ford',
year: '2020',
country: 'england'
},
{
name: 'Edgar',
type: 'bmw',
year: '2020',
country: 'belgium'
},
{
name: 'John',
type: 'bmw, audi',
year: '2019',
country: 'england'
},
{
name: 'Axel',
type: 'mercedes',
year: '2020',
country: 'england'
}
],
},
https://jsfiddle.net/yrbs2650/1/
提前致謝!
uj5u.com熱心網友回復:
關于“外部”javascript檔案的問題有點不清楚,我假設您的意思是它仍然存在于源代碼中,但存在于不同的檔案中。
在 Vue 檔案中使用 import 陳述句,在 js 檔案中使用 export 陳述句
專案.js
const items = [
{
name: 'Nolan',
type: 'mercedes, ford',
year: '2020',
country: 'england'
},
{
name: 'Edgar',
type: 'bmw',
year: '2020',
country: 'belgium'
},
{
name: 'John',
type: 'bmw, audi',
year: '2019',
country: 'england'
},
{
name: 'Axel',
type: 'mercedes',
year: '2020',
country: 'england'
}
];
export default items;
應用程式.vue
import items from './items.js';
new Vue({
el: '#app',
data: {
selectedType: '',
selectedCountry: '',
selectedYear: '',
items: items,
}
...
根據您放置 items.js 檔案的位置,將更改匯入路徑,如果它是您的 app.vue 的鄰居,那么 ./items.js 將起到作用。
JSON 格式是存盤此類資料的首選。 在這里閱讀
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481876.html
標籤:javascript Vue.js 十一
上一篇:在Vue中單擊內部鏈接時防止父級的onclick事件
下一篇:使用地圖功能驗證資料
