Kbone基础 Kbone + Vue 项目手工搭建流程

    assetNameRegExp: /\.(css|wxss)$/g,
    cssProcessor: require('cssnano'),
    cssProcessorPluginOptions: {
      preset: ['default', {
        discardComments: {
          removeAll: true,
        },
        minifySelectors: false, // 因为 wxss 编译器不支持 .some>:first-child 这样格式的代码,所以暂时禁掉这个
      }],
    },
    canPrint: false
  }),
  // 压缩 js
  new TerserPlugin({
    test: /\.js(\?.*)?$/i,
    parallel: true,
  })
] : [],

},

module: {
rules: [
{
test: /.cssKaTeX parse error: Expected 'EOF', got '}' at position 98: … ], }̲, { …/,
loader: ‘vue-loader’,
},
{
test: /.jsKaTeX parse error: Expected 'EOF', got '}' at position 93: …modules/ }̲, { …/,
loader: ‘file-loader’,
options: {
name: ‘[name].[ext]?[hash]’
}
}
]
},

resolve: {
extensions: [‘*’, ‘.js’, ‘.vue’, ‘.json’]
},

plugins: [
new webpack.DefinePlugin({
‘process.env.isMiniprogram’: process.env.isMiniprogram, // 注入环境变量,用于业务代码判断
}),
new MiniCssExtractPlugin({
filename: ‘[name].wxss’,
}),
new VueLoaderPlugin(),
new MpPlugin(require(‘./miniprogram.config.js’)),
],
}


#### 2.2 安装依赖


安装上述配置文件里的 loader 和 plugin 依赖:



npm install babel-loader @babel/core css-loader file-loader mini-css-extract-plugin vue-loader vue-template-compiler optimize-css-assets-webpack-plugin terser-webpack-plugin mp-webpack-plugin --save-dev


#### 2.3 编写 webpack 插件配置


这里的 webpack 插件配置即 MpPlugin 的配置参数文件。在 build 文件夹下创建 miniprogram.config.js 文件,内容如下:



module.exports = {
// 页面 origin,默认是 https://miniprogram.default
origin: ‘https://test.miniprogram.com’,

// 入口页面路由
entry: ‘/test/aaa’,

// 页面路由,用于页面间跳转。其值是一个以页面名称作为 key 的对象,每项的值是该页面可以响应的路由
router: {
index: [
‘/test/aaa’,
‘/test/bbb’,
],
},

// 特殊路由跳转
redirect: {
notFound: ‘index’,
accessDenied: ‘index’,
},

// 构建输出配置
generate: {
/**
* 注入全局变量,每一项为 [key, value] 的结构。构建时会将需要注入的全局变量声明在所有要执行的代码之前,以方便代码里直接使用。
* 如果配置了 [‘TEST_VAR_STRING’, ‘‘miniprogram’’],则会生成类似 var TEST_VAR_STRING = ‘miniprogram’ 的声明语句;
* 不指定 value 的话,则会从 window 下读取,如 [‘CustomEvent’] 则会生成类似 var CustomEvent = window.CustomEvent 的声明语句。
*/
globalVars: [
[‘TEST_VAR_STRING’, ‘‘miniprogram’’],
[‘TEST_VAR_NUMBER’, ‘123’],
[‘TEST_VAR_BOOL’, ‘true’],
[‘TEST_VAR_FUNCTION’, ‘function() {return ‘I am function’}’],
[‘TEST_VAR_OTHERS’, ‘window.document’],
[‘open’],
],

// 构建完成后是否自动安装小程序依赖。'npm':使用 npm 自动安装依赖
    autoBuildNpm: 'npm',

},

// 小程序全局配置,参见 https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/app.html#window
app: {
navigationBarTitleText: ‘kbone-vue-project’,
},

// 所有页面的全局配置
global: {
rem: true, // 是否支持 rem
pageStyle: true, // 是否支持修改页面样式
},

// 项目配置,会被合并到 project.config.json 中
projectConfig: {
appid: ‘’,
projectname: ‘kbone-vue-project’,
},

// 包配置,会被合并到 package.json 中
packageConfig: {
author: ‘wechat-miniprogram’,
},
}


#### 3、新增入口文件


**3.1 在项目根目录下创建** **`src/index`** **目录,在** **`index`** **目录下创建** **`main.mp.js`文件:**



import Vue from ‘vue’
import Router from ‘vue-router’
import App from ‘./App.vue’
import AAA from ‘./AAA.vue’
import BBB from ‘./BBB.vue’

export default function createApp() {
const container = document.createElement(‘div’)
container.id = ‘app’
document.body.appendChild(container)

// rem 和页面样式修改
window.onload = function() {
document.documentElement.style.fontSize = wx.getSystemInfoSync().screenWidth / 16 + ‘px’
document.documentElement.style.backgroundColor = ‘#fffbe7’
}
window.onerror = (message, source, lineno, colno, error) => {
console.log('window.onerror => ', message, source, lineno, colno, error)
};
window.addEventListener(‘error’, evt => console.log(‘window.addEventListener(‘error’) =>’, evt))

Vue.use(Router)

const router = new Router({
mode: ‘history’, // 是否使用 history api
routes: [
{ path: ‘/test/aaa’, component: AAA },
{ path: ‘/test/bbb’, component: BBB }
]
})

return new Vue({
el: ‘#app’,
router,
render: h => h(App)
})
}


**3.2 安装 Vue Vue-router**



npm install vue vue-router


#### 4、构建项目文件


**4.1 创建App.vue**


在 `index` 目录下创建 `App.vue` 文件,实现了:


* 路由组件的展示和路由切换
* 载入子组件
* 全局变量的测试
* cookie的测试
* 抛出异常的测试



  • aaa
  • bbb

**4.2 创建 Footer.vue组件**


在 `src` 目录下创建 `common` 目录,在 `common` 目录下创建 `Footer.vue` 文件:



<sc

真题解析、进阶学习笔记、最新讲解视频、实战项目源码、学习路线大纲
详情关注公中号【编程进阶路】

ript>
export default {}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值