如何在外部获取vue对象,如何在vue.js应用程序中访问外部json文件对象

How to access JSON objects in the vue.js app I am new in this

import json from './json/data.json'

the JSON file is loaded and now I have to access the objects within it

解决方案

Just assign the import to a data property

import json from './json/data.json'

export default{

data(){

return{

myJson: json

}

}

}

then loop through the myJson property in your template using v-for

{{data}}

NOTE

If the object you want to import is static i.e does not change then assigning it to a data property would make no sense as it does not need to be reactive.

Vue converts all the properties in the data option to getters/setters for the properties to be reactive. So it would be unnecessary and overhead for vue to setup getters/setters for data which is not going to change. See Reactivity in depth.

So you can create a custom option as follows:

import MY_JSON from './json/data.json'

export default{

//custom option named myJson

myJson: MY_JSON

}

then loop through the custom option in your template using $options:

{{data}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值