advanced installer更換程序id_好程序員web前端培訓分享kbone高級-事件系統

  好程序員web前端培訓分享kbone高級-事件系統:1、用法,對于多頁面的應用,在 Web 端可以直接通過 a 標簽或者 location 對象進行跳轉,但是在小程序中則行不通;同時 Web 端的頁面 url 實現和小程序頁面路由也是完全不一樣的,因此對于多頁開發最大的難點在于如何進行頁面跳轉。

3d012892e3538608914177b6bffffdcd.png

  1.1 修改 webpack 配置

  對于多頁應用,此處和 Web 端一致,有多少個頁面就需要配置多少個入口文件。如下例子,這個應用中包含 page1、page2 和 page2 三個頁面:

// webpack.mp.config.js

module.exports = {

entry: {

page1: path.resolve(__dirname, '../src/page1/main.mp.js'),

page2: path.resolve(__dirname, '../src/page2/main.mp.js'),

page3: path.resolve(__dirname, '../src/page3/main.mp.js'),

},

// ... other options

}

1.2 修改 webpack 插件配置

mp-webpack-plugin 這個插件的配置同樣需要調整,需要開發者提供各個頁面對應的 url 給 kbone。

module.exports = {

origin: 'https://test.miniprogram.com',

entry: '/page1',

router: {

page1: ['/(home|page1)?', '/test/(home|page1)'],

page2: ['/test/page2/:id'],

page3: ['/test/page3/:id'],

},

// ... other options

}

其中 origin 即 window.location.origin 字段,使用 kbone 的應用所有頁面必須同源,不同源的頁面禁止訪問。entry 頁面表示這個應用的入口 url。router 配置則是各個頁面對應的 url,可以看到每個頁面可能不止對應一個 url,而且這里的 url 支持參數配置。

有了以上幾個配置后,就可以在 kbone 內使用 a 標簽或者 location 對象進行跳轉。kbone 會將要跳轉的 url 進行解析,然后根據配置中的 origin 和 router 查找出對應的頁面,然后拼出頁面在小程序中的路由,最后通過小程序 API 進行跳轉(利用 wx.redirectTo 等方法)。

2、案例

在 kbone-advanced 目錄下創建 02-mulpages 目錄。本案例在這個目錄下實現。

2.1 創建 package.json

cd 02-mulpages

npm init -y

編輯 package.json:

{

"name": "01-env",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": {

"mp": "cross-env NODE_ENV=production webpack --config build/webpack.mp.config.js --progress --hide-modules"

},

"dependencies": {

"add": "^2.0.6",

"vue": "^2.5.11"

},

"browserslist": [

"> 1%",

"last 2 versions",

"not ie <= 8"

],

"devDependencies": {

"babel-core": "^6.26.0",

"babel-loader": "^7.1.2",

"babel-preset-env": "^1.6.0",

"cross-env": "^5.0.5",

"css-loader": "^0.28.7",

"file-loader": "^1.1.4",

"html-webpack-plugin": "^4.0.0-beta.5",

"mini-css-extract-plugin": "^0.5.0",

"optimize-css-assets-webpack-plugin": "^5.0.1",

"stylehacks": "^4.0.3",

"vue-loader": "^15.7.0",

"vue-template-compiler": "^2.6.10",

"webpack": "^4.29.6",

"webpack-cli": "^3.2.3",

"mp-webpack-plugin": "latest"

},

"keywords": [],

"author": "",

"license": "ISC"

}

安裝依賴包:

npm install

2.2 配置 webpack

在 02-mulpages 目錄下創建 build 文件夾,在文件夾下創建 webpack.mp.config.js 文件,內容如下:

const path = require('path')

const webpack = require('webpack')

const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const { VueLoaderPlugin } = require('vue-loader')

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

const TerserPlugin = require('terser-webpack-plugin')

const MpPlugin = require('mp-webpack-plugin') // 用于構建小程序代碼的 webpack 插件

const isOptimize = false // 是否壓縮業務代碼,開發者工具可能無法完美支持業務代碼使用到的 es 特性,建議自己做代碼壓縮

module.exports = {

mode: 'production',

entry: {

page1: path.resolve(__dirname, '../src/page1/main.mp.js'),

page2: path.resolve(__dirname, '../src/page2/main.mp.js'),

page3: path.resolve(__dirname, '../src/page3/main.mp.js'),

},

output: {

path: path.resolve(__dirname, '../dist/mp/common'), // 放到小程序代碼目錄中的 common 目錄下

filename: '[name].js', // 必需字段,不能修改

library: 'createApp', // 必需字段,不能修改

libraryExport: 'default', // 必需字段,不能修改

libraryTarget: 'window', // 必需字段,不能修改

},

target: 'web', // 必需字段,不能修改

optimization: {

runtimeChunk: false, // 必需字段,不能修改

splitChunks: { // 代碼分隔配置,不建議修改

chunks: 'all',

minSize: 1000,

maxSize: 0,

minChunks: 1,

maxAsyncRequests: 100,

maxInitialRequests: 100,

automaticNameDelimiter: '~',

name: true,

cacheGroups: {

vendors: {

test: /[/]node_modules[/]/,

priority: -10

},

default: {

minChunks: 2,

priority: -20,

reuseExistingChunk: true

}

}

},

minimizer: isOptimize ? [

// 壓縮CSS

new OptimizeCSSAssetsPlugin({

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: /.css$/,

use: [

MiniCssExtractPlugin.loader,

'css-loader'

],

},

{

test: /.vue$/,

loader: [

'vue-loader',

],

},

{

test: /.js$/,

use: {

loader: 'babel-loader',

options: {

presets: ['env']

}

},

exclude: /node_modules/

},

{

test: /.(png|jpg|gif|svg)$/,

loader: 'file-loader',

options: {

name: '[name].[ext]?[hash]'

}

}

]

},

resolve: {

extensions: ['*', '.js', '.vue', '.json']

},

plugins: [

new webpack.DefinePlugin({

'process.env.isMiniprogram': true, // 注入環境變量,用于業務代碼判斷

}),

new MiniCssExtractPlugin({

filename: '[name].wxss',

}),

new VueLoaderPlugin(),

new MpPlugin(require('./miniprogram.config.js')),

],

}

在 02-mulpages/build 文件夾下創建 miniprogram.config.js 文件,內容如下:

module.exports = {

origin: 'https://test.miniprogram.com',

entry: '/',

router: {

page1: ['/a'],

page2: ['/b'],

page3: ['/c'],

},

redirect: {

notFound: 'page1',

accessDenied: 'page1',

},

generate: {

appEntry: 'miniprogram-app',

// 構建完成后是否自動安裝小程序依賴。'npm':使用 npm 自動安裝依賴

autoBuildNpm: 'npm'

},

runtime: {

cookieStore: 'memory',

},

app: {

navigationBarTitleText: 'kbone-multiple-pages',

},

global: {

share: true,

},

pages: {

page1: {

extra: {

navigationBarTitleText: 'page1',

},

},

},

projectConfig: {

appid: '',

projectname: 'kbone-multiple-pages',

},

packageConfig: {

author: 'Felixlu',

}

}

2.3 編寫三個頁面

在 /src/ 下創建 page1, page2, page3 三個文件夾,在文件夾里創建三個頁面,每個頁面由 App.vue 和 main.mp.js 兩個文件組成。

1、page1 頁面

/src/page1/App.vue 內容:

當前 url:{{url}}

當前頁跳轉

新開頁面跳轉

當前頁跳轉

新開頁面跳轉

.cnt {

margin-top: 20px;

}

a, button {

display: block;

width: 100%;

height: 30px;

line-height: 30px;

text-align: center;

font-size: 20px;

border: 1px solid #ddd;

}

/src/page1/main.mp.js 內容:

import Vue from 'vue'

import App from './App.vue'

export default function createApp() {

const container = document.createElement('div')

container.id = 'app'

document.body.appendChild(container)

return new Vue({

el: '#app',

render: h => h(App)

})

}

/src/common/Header.vue 內容:

wechat-miniprogram-header

.header {

margin-bottom: 10px;

width: 100%;

text-align: center;

}

/src/common/utils.js 內容:

export function printf(str) {

console.log('common/utils.js --> ', str)

}

/src/common/Footer.vue 內容:

.footer {

margin-top: 10px;

width: 100%;

text-align: center;

}

2、page2 頁面

/src/page2/App.vue 內容:

當前 url:{{url}}

回到首頁

回到首頁

relaunch

.cnt {

margin-top: 20px;

}

a, button {

display: block;

width: 100%;

height: 30px;

line-height: 30px;

text-align: center;

font-size: 20px;

border: 1px solid #ddd;

}

/src/page2/main.mp.js 內容:

import Vue from 'vue'

import App from './App.vue'

export default function createApp() {

const container = document.createElement('div')

container.id = 'app'

document.body.appendChild(container)

return new Vue({

el: '#app',

render: h => h(App)

})

}

3、page3 頁面

/src/page3/App.vue 內容:

當前 url:{{url}}

回到上一頁

關閉當前窗口

.cnt {

margin-top: 20px;

}

a, button {

display: block;

width: 100%;

height: 30px;

line-height: 30px;

text-align: center;

font-size: 20px;

border: 1px solid #ddd;

}

/src/page3/main.mp.js 內容:

import Vue from 'vue'

import App from './App.vue'

export default function createApp() {

const container = document.createElement('div')

container.id = 'app'

document.body.appendChild(container)

return new Vue({

el: '#app',

render: h => h(App)

})

}

2.4 小程序端效果預覽

npm run mp

24329e16e408afc65db843bc26dde815.png
58cd41a6e284274f7d53ca97cbc3ccbd.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389089.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389089.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389089.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ai對話機器人實現方案_顯然地引入了AI —無代碼機器學習解決方案

ai對話機器人實現方案A couple of folks from Obviously.ai contacted me a few days back to introduce their service — a completely no-code machine learning automation tool. I was a bit skeptical at first, as I always am with supposedly fully-automated solutio…

網絡負載平衡的

網絡負載平衡允許你將傳入的請求傳播到最多達32臺的服務器上&#xff0c;即可以使用最多32臺服務器共同分擔對外的網絡請求服務。網絡負載平衡技術保證即使是在負載很重的情況下它們也能作出快速響應。 網絡負載平衡對外只須提供一個IP地址&#xff08;或域名&#xff09;。 如…

透明狀態欄導致windowSoftInputMode:adjustResize失效問題

當我們通過下面代碼&#xff1a; getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 設置狀態欄透明&#xff0c;當界面存在EditText時&#xff0c;在activity里面設置windowSoftInputMode:…

[TimLinux] JavaScript 元素動態顯示

1. css的opacity屬性 這個屬性用于&#xff1a;設置元素的不透明級別&#xff0c;取值范圍&#xff1a;從 0.0 &#xff08;完全透明&#xff09;到 1.0&#xff08;完全不透明&#xff09;&#xff0c;元素所在的文本流還在。這個屬性的動態變化可以用來設置元素的淡入淡出效果…

神經網絡 CNN

# encodingutf-8import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_datamnist input_data.read_data_sets(MNIST_data, one_hotTrue)def weight_variable(shape): initial tf.truncated_normal(shape, stddev0.1) # 定義…

圖片中的暖色或冷色濾色片是否會帶來更多點擊? —機器學習A / B測試

A/B test on ads is the art of choosing the best advertisement that optimizes your goal (number of clicks, likes, etc). For example, if you change a simple thing like a filter in your pictures you will drive more traffic to your links.廣告的A / B測試是一種選…

3d制作中需要注意的問題_淺談線路板制作時需要注意的問題

PCB電路板是電子設備重要的基礎組裝部件&#xff0c;在制作PCB電路板時&#xff0c;只有將各個方面都考慮清楚&#xff0c;才能保證電子設備在使用時不會出現問題。今天小編就與大家一起分享線路板制作時需要注意的問題&#xff0c;歸納一下幾點&#xff1a;1、考慮制作類型電路…

冷啟動、熱啟動時間性能優化

用戶希望應用程序能夠快速響應并加載。 一個啟動速度慢的應用程序不符合這個期望&#xff0c;可能會令用戶失望。 這種糟糕的體驗可能會導致用戶在應用商店中對您的應用進行糟糕的評價&#xff0c;甚至完全放棄您的應用。 本文檔提供的信息可幫助您優化應用的啟動時間。 它首先…

python:lambda、filter、map、reduce

lambda 為關鍵字。filter&#xff0c;map&#xff0c;reduce為內置函數。 lambda&#xff1a;實現python中單行最小函數。 g lambda x: x * 2 #相當于 def g(x):return x*2print(g(3))# 6 注意&#xff1a;這里直接g(3)可以執行&#xff0c;但沒有輸出的&#xff0c;前面的…

集群

原文地址&#xff1a;http://www.microsoft.com/china/MSDN/library/windev/COMponentdev/CdappCEnter.mspx?mfrtrue 本文假設讀者熟悉 Windows 2000、COM、IIS 5.0 摘要 Application Center 2000 簡化了從基于 Microsoft .NET 的應用程序到群集的部署&#xff0c;群集是一組…

Myeclipes連接Mysql數據庫配置

相信大家在網站上也找到了許多關于myeclipes如何連接mysql數據庫的解決方案&#xff0c;雖然每一步都按照他的步驟來&#xff0c;可到最后還是提示連接失敗&#xff0c;有的方案可能應個人設備而異&#xff0c;配置環境不同導致。經過個人多方探索終于找到一個簡單便捷的配置方…

cnn圖像二分類 python_人工智能Keras圖像分類器(CNN卷積神經網絡的圖片識別篇)...

上期文章我們分享了人工智能Keras圖像分類器(CNN卷積神經網絡的圖片識別的訓練模型)&#xff0c;本期我們使用預訓練模型對圖片進行識別&#xff1a;Keras CNN卷積神經網絡模型訓練導入第三方庫from keras.preprocessing.image import img_to_arrayfrom keras.models import lo…

圖卷積 節點分類_在節點分類任務上訓練圖卷積網絡

圖卷積 節點分類This article goes through the implementation of Graph Convolution Networks (GCN) using Spektral API, which is a Python library for graph deep learning based on Tensorflow 2. We are going to perform Semi-Supervised Node Classification using C…

[微信小程序] 當動畫(animation)遇上延時執行函數(setTimeout)出現的問題

小程序中當動畫animation遇上setTimeout函數內部使用this.setData函數&#xff0c;通常情況下會出現報錯。本文先告訴解決方法&#xff0c;后分析報錯原因 1.解決方法&#xff1a; 在 setTimeout() 函數的同級加上 const that this; &#xff0c;然后將this.setData換成that…

關于使用pdf.js預覽pdf的一些問題

手機應用中pdf展示使用非常廣泛&#xff0c; 一些pdf由于特殊的內容比如文字、電子簽章必須使用復雜的解析器來解析&#xff0c;當使用MultiPdf 這個庫加載&#xff0c;會使得包變得非常龐大&#xff0c; 這里我們考慮使用pdf.js 來解析pdf. 引用非常簡單&#xff0c;只需要把…

SqlHelper改造版本

using System;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Collections; /// <summary> /// SqlHelper類是專門提供給廣大用戶用于高性能、可升級和最佳練習的sql數據操作 /// </summary> public abstract c…

回歸分析預測_使用回歸分析預測心臟病。

回歸分析預測As per the Centers for Disease Control and Prevention report, heart disease is the prime killer of both men and women in the United States and around the globe. There are several data mining techniques that can be leveraged by researchers/ stat…

VMware文件共享

VMware tools 文件共享 已經安裝后&#xff1a; vmhgfs-fuse .host:/ /mnt/hgfs

npm 問題(一)

今天在使用npm安裝程序時出現了以下問題如下&#xff1a; 我解決了問題&#xff0c;這是由于緩存清除錯誤&#xff08;但他們自動修復&#xff09;有一些數據損壞&#xff0c;沒有讓JSON文件解析&#xff0c;使用以下命令可以解決&#xff1a;即&#xff1a; npm cache clean -…

UDP打洞程序包的源碼

C#實現UDP打洞 轉自&#xff1a;http://hi.baidu.com/sdfiyon/blog/item/63a6e039155e02f23a87ceb1.html 下面是UDP打洞程序包的源碼&#xff1a;//WellKnown公用庫using System;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Net ;usi…