请问在js文件中能否重新import?
发布于 4 年前 作者 yongzhong 4495 次浏览 来自 问答

大家好,谢谢你们进来帮忙解决我的疑问。我的问题如下,最近希望做一个能切换中英文的小程序。

之前也从csdn上找到相关的方法,也是能成功使用。但是最近有一个新的想法,就是希望数据能独立管理。所以我写了一个单独的js文件来管理数据。

const app = getApp()
const {gin,tonicWater,coke,sweetVermouth,campari,angosturaBitter,orangeBitter,whiskey,bourbon,rye,limeWedge,orangePeel,brandiedCherry,oz,dash,dashes} = app.globalData.content
export const cocktails = [
  {id:1, name: 'Gin and Tonic', difficulty:1,
  ingredients_vol:[2,6] ,
  ingredients_unit:[oz,oz] ,
  ingredients:[gin, tonicWater], 
  garnish:limeWedge},
  {id:2, name: 'Whiskey and Coke', difficulty:1, 
  ingredients_vol:[2, 4], 
  ingredients_unit:[oz, oz],
  ingredients:[whiskey, coke], 
  garnish:limeWedge},
]

然后在index.js里面调用这个文件

const app = getApp()
import {cocktails} from '../data/cocktails'
Page({
  data: {
    cocktails,
    cocktail: cocktails[0],
    transformIdx: 0,
    position: 'center',
    duration: 300,
    show: false,
    overlay: false
  },

但是每次我点击切换语言的时候,因为cocktails.js并没有获取新的语言信息,所以无法更新成新的语言。但是如果我切换语言后在开发者工具重新编译后,它却能显示正确的语言。所以我希望看看有没有能重新import的功能或者怎么样能够实现我这个想法。

谢谢你们!

2 回复

切换逻辑是怎么写的呢? 建议试试切换时用过setData方法重新触发渲染试试。

回到顶部