千家信息网

怎么用kbone实现跨页面通信和跨页面数据共享

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容介绍了"怎么用kbone实现跨页面通信和跨页面数据共享"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
千家信息网最后更新 2026年10月31日怎么用kbone实现跨页面通信和跨页面数据共享

本篇内容介绍了"怎么用kbone实现跨页面通信和跨页面数据共享"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.1 在页面中订阅广播消息

// 页面1
window.$$subscribe('hello', data => {
console.log('receive a msg: ', data)
})

1.2 在其他页面中发布广播消息

// 页面2
window.$$publish('hello', 'I am june')

在订阅了此消息的页面则会输出 receive a msg: I am june。

PS:如果需要取消订阅消息,可以使用 window.$$unsubscribe 接口进行取消。
PS:页面关闭后,会取消该页面所有的订阅。

如果需要跨页面数据进行共享,可以使用 window.$$global 对象,所有页面的 window.$$global 均会指向同一个对象:

// 页面1
window.$$global.name = 'june'

// 页面2
console.log(window.$$global.name) // 输出 june

PS:具体 API 可参考 dom/bom 扩展 API 文档。

2、案例

在 kbone-advanced 目录下创建 08-share-message 目录,本案例在这个目录下完成。

2.1 创建 package.json

cd 08-share-message
npm init -y

编辑 package.json:

{
"scripts": {
"mp": "cross-env NODE_ENV=production webpack --config build/webpack.mp.config.js --progress --hide-modules"
},
"dependencies": {
"vue": "^2.5.11",
"vuex": "^3.1.3"
},
"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",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.2",
"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"
}
}

安装依赖包:

npm install

2.2 配置 webpack

在 08-share-message/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'),
page4: path.resolve(__dirname, '../src/page4/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', 'stage-3'],
},
}],
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': process.env.isMiniprogram, // 注入环境变量,用于业务代码判断
}),
new MiniCssExtractPlugin({
filename: '[name].wxss',
}),
new VueLoaderPlugin(),
new MpPlugin(require('./miniprogram.config.js')),
],
}

在 08-share-message/build 目录下创建 miniprogram.config.js,内容如下:

module.exports = {    
origin: 'https://test.miniprogram.com',
entry: '/',
router: {
page1: ['/'],
page2: ['/page2'],
page3: ['/page3'],
page4: ['/page4'],
},
redirect: {
notFound: 'page1',
accessDenied: 'page1',
},
generate: {
// 构建完成后是否自动安装小程序依赖。'npm':使用 npm 自动安装依赖
autoBuildNpm: 'npm'
},
app: {
navigationBarTitleText: 'miniprogram-project',
},
projectConfig: {
appid: '',
projectname: 'kbone-demo22',
},
packageConfig: {
author: 'wechat-miniprogram',
},
}

2.3 创建page1页面

在 /src/ 下创建 page1 文件夹,在 page1 下创建 main.mp.js 文件,内容如下:

import Vue from 'vue'
import {createStore} from '../store'
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',
store: createStore(),
render: h => h(App)
})
}

在 /src/ 下创建 store 文件夹,在 store 下创建 index.js 文件,内容如下:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export function createStore() {
const state = window.$$global.state || {
count: 0,
data: {},
}
window.$$global.state = state

return new Vuex.Store({
state,
actions: {
FETCH_DATA: ({commit}, data) => {
setTimeout(() => {
commit('SET_DATA', data)
}, 100)
},
},
mutations: {
SET_DATA: (state, data) => {
state.count++
Vue.set(state.data, 'name', data.name)
},
},
});
}

在 /src/page1 下创建 App.vue 文件,内容如下:






在 /src/ 下创建 common 文件夹,在 common 下创建 Header.vue 文件,内容如下:






在 /src/common 下创建 Footer.vue 文件,内容如下:






2.4 创建page2页面

在 /src/ 下创建 page2 文件夹,在 page2 下创建 main.mp.js 文件,内容如下:

import Vue from 'vue'
import {createStore} from '../store'
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',
store: createStore(),
render: h => h(App)
})
}

在 /src/page2 下创建 App.js 文件,内容如下:






2.5 创建page3页面

在 /src/ 下创建 page3 文件夹,在 page3 下创建 main.mp.js 文件,内容如下:

import Vue from 'vue'
import {createStore} from '../store'
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',
store: createStore(),
render: h => h(App)
})
}

在 /src/page3 下创建 App.js 文件,内容如下:






2.6 创建page4页面

在 /src/ 下创建 page4 文件夹,在 page4 下创建 main.mp.js 文件,内容如下:

import Vue from 'vue'
import {createStore} from '../store'
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',
store: createStore(),
render: h => h(App)
})
}

在 /src/page4 下创建 App.js 文件,内容如下:






2.7 小程序端效果预览

npm run mp

"怎么用kbone实现跨页面通信和跨页面数据共享"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

页面 文件 消息 内容 首页 代码 目录 字段 文件夹 数据 订阅 业务 程序 上一 输出 通信 对象 建议 更多 案例 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 花魁咖啡 江苏根荣互联网科技 厦门中拓互联网络科技有限公司 深圳齐家互联网科技 新浪科技互联网 天下捷融互联网科技 互联网科技大会案例 衡水科技产业互联网平台 随着互联网科技发展 北京华百购互联网科技有限公司 晓花互联网科技有限公司网址 苏州铭阳互联网科技有限公司 恒生互联网科技业指数调整周期 美国互联网科技实力 2021互联网科技公司排名 智帮互联网科技公司最近动向 互联网风格科技公司名称 抖集互联网科技技术咨询 容联科技互联网电商 科技互联网专业工作会议发言稿 喔噻互联网科技有限公司 易通互联网络科技有限公司 佰商互联网科技有限公司 互联网科技大会案例 互联网科技中介 福州新华互联网科技学校 知乎 北京华百购互联网科技有限公司 互联网科技接地气 盐城市攀科互联网科技有限公司 利用互联网等科技手段
0