前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue 实践记录

vue 实践记录

作者头像
易墨
发布2018-09-14 15:56:47
4870
发布2018-09-14 15:56:47
举报

打包后使用相对路径

build/webpack.prod.conf.js 的 output 节点添加配置:publicPath: './'

打包时使用shell复制文件

在入口 build/build.js 中使用.

  1. 引入 shelljs库 require('shelljs/global')
  2. 使用示例:cp('-R', 'favicon.ico', config.build.assetsRoot)

不同环境使用不同模式加载路由( vue 开发环境不适用懒加载)

router目录结构

  • _import_production.js 代码 module.exports = file => () => import('@/views/' + file + '.vue')
  • _import_testing.js 代码 module.exports = file => () => import('@/views/' + file + '.vue')
  • _import_development.js 代码 module.exports = file => require('@/views/' + file + '.vue').default
  • 路由中使用
代码语言:javascript
复制
const _import = require('./_import_' + process.env.NODE_ENV)
...
        component: _import('dashboard/index')
...

使用 require.context自动加载模块

使用:const files = require.context(directory, useSubdirectories, regExp)

参数说明

  • directory:说明需要检索的目录
  • useSubdirectories:是否检索子目录
  • regExp: 匹配文件的正则表达式

返回结果

files.keys(): 符合条件的文件路径集合

使用

获取当前目录所有 js 文件并获取导出模块

代码语言:javascript
复制
const files = require.context('.', true, /\.js/)
const modules = {}
files.keys().forEach(key => {
  if (key === './index.js') {
    return
  }
  var mk = key.replace(/(^\.\/|\.js$)/g, '')
  var m = files(key)
  modules[mk] = Object.keys(m).reduce((s, e) => {
    if (e !== 'default') {
      s[e] = m[e]
    }
    return s
  }, m.default||{})
})
//console.log(modules)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 打包后使用相对路径
  • 打包时使用shell复制文件
  • 不同环境使用不同模式加载路由( vue 开发环境不适用懒加载)
    • router目录结构
    • 使用 require.context自动加载模块
      • 参数说明
        • 返回结果
          • 使用
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档