文件后缀为.js 类型
在 js 文件中写入 json 数据,使用 export 导出。在需要的页面中用 import 方式进行导入。实现如下:
① 在项目 static 目录下,新建一个目录 data。
② 在 data 目录下,新建一个 icon.js 文件,注意文件后缀为 js。
③ 在 icon.js 页面中写上本地模拟 json 数据,并导出。
④ 在 index.vue 页面引入并使用。
- icon.js 代码
const iconColor = [
{
color: '#f56c6c'
}, {
color: 'yellow'
}, {
color: 'green'
}, {
color: 'orange'
}]
export {
iconColor
}
- 页面上使用
<template>
<view v-for="(item, index) in colorList">
<u-button :color="item.color"></u-button>
</view>
</template>
<script setup>
import { iconColor } from "@/static/data/icon.js"
const colorList = ref(iconColor)
</script>
