├── static └── .gitkeep ├── config ├── prod.env.js ├── dev.env.js └── index.js ├── screenshots ├── back.gif └── native.gif ├── src ├── assets │ └── logo.png ├── App.vue ├── main.js ├── router │ └── index.js ├── components │ ├── Sec.vue │ └── HelloWorld.vue └── common │ ├── app.js │ └── optimizeApp.js ├── .editorconfig ├── .gitignore ├── .babelrc ├── .postcssrc.js ├── index.html ├── README.md └── package.json /static/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /config/prod.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | module.exports = { 3 | NODE_ENV: '"production"' 4 | } 5 | -------------------------------------------------------------------------------- /screenshots/back.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lhb11/native-js-interactive-encapsulation/HEAD/screenshots/back.gif -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lhb11/native-js-interactive-encapsulation/HEAD/src/assets/logo.png -------------------------------------------------------------------------------- /screenshots/native.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lhb11/native-js-interactive-encapsulation/HEAD/screenshots/native.gif -------------------------------------------------------------------------------- /config/dev.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const merge = require('webpack-merge') 3 | const prodEnv = require('./prod.env') 4 | 5 | module.exports = merge(prodEnv, { 6 | NODE_ENV: '"development"' 7 | }) 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | /dist/ 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Editor directories and files 9 | .idea 10 | .vscode 11 | *.suo 12 | *.ntvs* 13 | *.njsproj 14 | *.sln 15 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "modules": false, 5 | "targets": { 6 | "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] 7 | } 8 | }], 9 | "stage-2" 10 | ], 11 | "plugins": ["transform-vue-jsx", "transform-runtime"] 12 | } 13 | -------------------------------------------------------------------------------- /.postcssrc.js: -------------------------------------------------------------------------------- 1 | // https://github.com/michael-ciniawsky/postcss-load-config 2 | 3 | module.exports = { 4 | "plugins": { 5 | "postcss-import": {}, 6 | "postcss-url": {}, 7 | // to edit target browsers: use "browserslist" field in package.json 8 | "autoprefixer": {} 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | native-js-interactive-encapsulation 7 | 8 | 9 |
10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 18 | 19 | 29 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | // The Vue build version to load with the `import` command 2 | // (runtime-only or standalone) has been set in webpack.base.conf with an alias. 3 | import Vue from 'vue' 4 | import App from './App' 5 | import router from './router' 6 | import vConsole from 'vconsole' 7 | 8 | 9 | // 导入封装的组件 10 | // import hybrid from './common/app'; 11 | // 使用组件绑定 12 | // Vue.use(hybrid, this); 13 | 14 | // 导入优化的封装类 15 | import opApp from './common/optimizeApp' 16 | console.log(window.$Hybrid); 17 | Vue.use(window.$Hybrid, this); 18 | 19 | Vue.config.productionTip = false 20 | // 手机页面上显示调试控制台 21 | var vConsole22 = new vConsole(); 22 | 23 | /* eslint-disable no-new */ 24 | new Vue({ 25 | el: '#app', 26 | router, 27 | components: { App }, 28 | template: '' 29 | }) 30 | -------------------------------------------------------------------------------- /src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Router from 'vue-router' 3 | import HelloWorld from '@/components/HelloWorld' 4 | import Sec from '@/components/Sec' 5 | 6 | Vue.use(Router) 7 | 8 | const routers = new Router({ 9 | routes: [ 10 | { 11 | path: '/', 12 | name: 'HelloWorld', 13 | component: HelloWorld 14 | }, 15 | { 16 | path: '/sec', 17 | name: 'Sec', 18 | component: Sec 19 | } 20 | ] 21 | }) 22 | 23 | routers.beforeEach((to, from, next) => { 24 | // console.log(to.path + '---' + from.path); 25 | // debugger 26 | // const dd = decodeURIComponent(to.path) 27 | // console.log(to.query); 28 | // console.log(dd); 29 | next(); 30 | }) 31 | 32 | routers.afterEach((to, from) => { 33 | // ... 34 | }) 35 | 36 | export default routers; 37 | 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 前言 2 | 3 | > vue和原生(ios和android)的交互封装 4 | 5 | > 主要在项目中使用到vue和原生的交互,原生交互的方法必须挂载到window上,引入vue的作用域,挂载到vue的方法原生无法访问到,所以写一个app.js来完成原生和js的交互 6 | 7 | > 目前主要使用的是监听安卓中的返回键事件 8 | 9 | > 监听html中和native的webview的事件交互 10 | 11 | 12 | ## 说明 13 | 14 | > 喜欢的别忘记了可以star一下的噢! 15 | 16 | > 开发环境 win10 Chrome 58.0.3029.110  nodejs 6.2.0 17 | 18 | ## 技术栈 19 | 20 | > vue + vue-router + webpack 21 | 22 | ## 效果图 23 | ## 原生调用组件中的事件演示 24 | 25 | 26 | ## 回掉原生事件演示 27 | 28 | 29 | 30 | 31 | ## 项目运行 32 | 33 | ``` bash 34 | # install dependencies 35 | npm install 36 | 37 | # serve with hot reload at localhost:8080 38 | npm run dev 39 | 40 | # build for production with minification 41 | npm run build 42 | 43 | # build for production and view the bundle analyzer report 44 | npm run build --report 45 | ``` -------------------------------------------------------------------------------- /src/components/Sec.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/components/HelloWorld.vue: -------------------------------------------------------------------------------- 1 | 12 | 13 | 54 | 55 | 56 | 72 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "native-js-interactive-encapsulation", 3 | "version": "1.0.0", 4 | "description": "vue和原生(ios和android)的交互封装", 5 | "author": "lhb", 6 | "private": true, 7 | "scripts": { 8 | "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", 9 | "start": "npm run dev", 10 | "build": "node build/build.js" 11 | }, 12 | "dependencies": { 13 | "vconsole": "^3.1.0", 14 | "vue": "^2.5.2", 15 | "vue-router": "^3.0.1" 16 | }, 17 | "devDependencies": { 18 | "autoprefixer": "^7.1.2", 19 | "babel-core": "^6.22.1", 20 | "babel-helper-vue-jsx-merge-props": "^2.0.3", 21 | "babel-loader": "^7.1.1", 22 | "babel-plugin-syntax-jsx": "^6.18.0", 23 | "babel-plugin-transform-runtime": "^6.22.0", 24 | "babel-plugin-transform-vue-jsx": "^3.5.0", 25 | "babel-preset-env": "^1.3.2", 26 | "babel-preset-stage-2": "^6.22.0", 27 | "chalk": "^2.0.1", 28 | "copy-webpack-plugin": "^4.0.1", 29 | "css-loader": "^0.28.0", 30 | "extract-text-webpack-plugin": "^3.0.0", 31 | "file-loader": "^1.1.4", 32 | "friendly-errors-webpack-plugin": "^1.6.1", 33 | "html-webpack-plugin": "^2.30.1", 34 | "node-notifier": "^5.1.2", 35 | "optimize-css-assets-webpack-plugin": "^3.2.0", 36 | "ora": "^1.2.0", 37 | "portfinder": "^1.0.13", 38 | "postcss-import": "^11.0.0", 39 | "postcss-loader": "^2.0.8", 40 | "postcss-url": "^7.2.1", 41 | "rimraf": "^2.6.0", 42 | "semver": "^5.3.0", 43 | "shelljs": "^0.7.6", 44 | "uglifyjs-webpack-plugin": "^1.1.8", 45 | "url-loader": "^0.5.8", 46 | "vue-loader": "^13.3.0", 47 | "vue-style-loader": "^3.0.1", 48 | "vue-template-compiler": "^2.5.2", 49 | "webpack": "^3.6.0", 50 | "webpack-bundle-analyzer": "^2.9.0", 51 | "webpack-dev-server": "^2.9.1", 52 | "webpack-merge": "^4.1.0" 53 | }, 54 | "engines": { 55 | "node": ">= 6.0.0", 56 | "npm": ">= 3.0.0" 57 | }, 58 | "browserslist": [ 59 | "> 1%", 60 | "last 2 versions", 61 | "not ie <= 8" 62 | ] 63 | } 64 | -------------------------------------------------------------------------------- /config/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | // Template version: 1.3.1 3 | // see http://vuejs-templates.github.io/webpack for documentation. 4 | 5 | const path = require('path') 6 | 7 | module.exports = { 8 | dev: { 9 | 10 | // Paths 11 | assetsSubDirectory: 'static', 12 | assetsPublicPath: '/', 13 | proxyTable: {}, 14 | 15 | // Various Dev Server settings 16 | host: 'localhost', // can be overwritten by process.env.HOST 17 | port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined 18 | autoOpenBrowser: false, 19 | errorOverlay: true, 20 | notifyOnErrors: true, 21 | poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- 22 | 23 | 24 | /** 25 | * Source Maps 26 | */ 27 | 28 | // https://webpack.js.org/configuration/devtool/#development 29 | devtool: 'cheap-module-eval-source-map', 30 | 31 | // If you have problems debugging vue-files in devtools, 32 | // set this to false - it *may* help 33 | // https://vue-loader.vuejs.org/en/options.html#cachebusting 34 | cacheBusting: true, 35 | 36 | cssSourceMap: true 37 | }, 38 | 39 | build: { 40 | // Template for index.html 41 | index: path.resolve(__dirname, '../dist/index.html'), 42 | 43 | // Paths 44 | assetsRoot: path.resolve(__dirname, '../dist'), 45 | assetsSubDirectory: 'static', 46 | assetsPublicPath: '/', 47 | 48 | /** 49 | * Source Maps 50 | */ 51 | 52 | productionSourceMap: true, 53 | // https://webpack.js.org/configuration/devtool/#production 54 | devtool: '#source-map', 55 | 56 | // Gzip off by default as many popular static hosts such as 57 | // Surge or Netlify already gzip all static assets for you. 58 | // Before setting to `true`, make sure to: 59 | // npm install --save-dev compression-webpack-plugin 60 | productionGzip: false, 61 | productionGzipExtensions: ['js', 'css'], 62 | 63 | // Run the build command with an extra argument to 64 | // View the bundle analyzer report after build finishes: 65 | // `npm run build --report` 66 | // Set to `true` or `false` to always turn it on or off 67 | bundleAnalyzerReport: process.env.npm_config_report 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/common/app.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 封装和原生(ios和android)的交互类, 3 | 可以在vue的类中访问交互,方法挂载到window上工给原生调用 4 | */ 5 | var hybrid = { 6 | // 默认是安卓,主要用于判断是安卓还是ios 7 | type: 'android', 8 | // 区分安卓和ios的不同调用方式 9 | native: window.app, 10 | // 用于vue安装这个插件 这个方法的第一个参数是 Vue 构造器 , 第二个参数是一个可选的选项对象: 11 | install (Vue, options) { 12 | // 把当前的对象挂载到vue的全局 13 | Vue.prototype.$hybrid = this; 14 | Vue.prototype.$eventHub= Vue.prototype.$eventHub || new Vue({data:{'message':''}, template:'
{{message}} '}); 15 | this.$eventHub = Vue.prototype.$eventHub; 16 | 17 | Vue.myGlobalMethod = function () { // 1. 添加全局方法或属性,如: vue-custom-element 18 | // 逻辑... 19 | console.log('我是全局的方法'); 20 | }; 21 | Vue.directive('my-directive', { // 2. 添加全局资源:指令/过滤器/过渡等,如 vue-touch 22 | bind (el, binding, vnode, oldVnode) { 23 | // 逻辑... 24 | console.log('我是全局的指令'); 25 | el.style.color = binding.value.color; 26 | el.innerHTML = binding.value.text; 27 | }, 28 | update (el, binding) { 29 | el.style.color = binding.value.color; 30 | el.innerHTML = binding.value.text; 31 | } 32 | }); 33 | Vue.mixin({ 34 | created: function () { // 3. 通过全局 mixin方法添加一些组件选项,如: vuex 35 | // 逻辑... 36 | // 合并一些东西到引入的组件内 37 | this.message = "我在封装的类内声明的变量"; 38 | } 39 | }); 40 | Vue.prototype.$myMethod = function (options) { // 4. 添加实例方法,通过把它们添加到 Vue.prototype 上实现 41 | // 逻辑... 42 | console.log('我是实例上的一个方法'); 43 | } 44 | }, 45 | // 主要用户初始化信息 46 | initType (params) { 47 | if (params == 'ios') { 48 | hybrid.type = "ios"; 49 | } 50 | console.log(hybrid.type); 51 | }, 52 | // 发送给原生的方法,...params : 这个参数就是一个数组 53 | send (...params) { 54 | console.log(params); 55 | }, 56 | // 注册原生发过来的方法,name需要注册的方法的名字 57 | registerHandler (name, callBack) { 58 | console.log(name + "js 处理原生发送的数据"); 59 | try { 60 | // eval(this.native.callBack) 必须指定清楚那个对象的函数名字 61 | if (typeof(eval(this.native.callBack)) == "function") { 62 | eval("this.native." + callBack + "('js处理完成之后的结果回传给原生客户端');"); 63 | } else { 64 | // 函数不存在, 抛出异常提示函数不存在 65 | alert(callBack); 66 | } 67 | }catch(err){ 68 | alert(err) // 可执行 69 | } 70 | }, 71 | appGoBack(index) { 72 | console.log("原生调用封装类" + index); 73 | //调用$emit 方法, 来发送全局的函数 74 | this.$eventHub.$emit('goBack', index); 75 | } 76 | } 77 | // 把vue中绑定的对象挂载到window上 78 | window.Hybrid = hybrid; 79 | // if (window.Vue) { // 自动绑定 80 | // window.Vue.use(hybrid); 81 | // } 82 | // 导出共给vue组件中引入使用 83 | export default hybrid -------------------------------------------------------------------------------- /src/common/optimizeApp.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 对app.js做优化处理 3 | * 自执行匿名函数 4 | * 常见格式:(function(){ code })(); 5 | 解释:包围函数(function(){ code })的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿名函数的参数。 6 | 作用:可以用它创建命名空间,只要把自己的所有代码都写在这个特殊的函数包装内,那么外部就不能访问,除非你允许(变量前加上window,这样该函数后变量就成为全局)。各JavaScript库的代码也基本上是这种组织形式。 7 | 总结一下,执行函数的作用主要为匿名和自动执行,代码在被解释时就已经在运行了。 8 | 9 | 写法总结: 10 | (function(){ code }()); 11 | !function(){ code }(); 12 | ~function(){ code }(); 13 | -function(){ code }(); 14 | +function(){ code }(); 15 | 16 | * 到处一个自调用函数,让一个函数声明语句变成了一个表达式。 17 | * 优点 :()和加减号在chrome表现惊人,而且在其他浏览器下也普遍很快,相比感叹号效果更好。 18 | */ 19 | export default (() => { 20 | window.$Hybrid = {}; // 声明一个window上的对象 21 | +function (global) { 22 | // 默认是安卓,主要用于判断是安卓还是ios 23 | global.$type = 'android'; 24 | // 区分安卓和ios的不同调用方式 25 | global.$native = window.app; 26 | // 用于vue安装这个插件 这个方法的第一个参数是 Vue 构造器 , 第二个参数是一个可选的选项对象: 27 | global.install = function (Vue, options) { 28 | // 把当前的对象挂载到vue的全局 29 | Vue.prototype.$hybrid = this; 30 | Vue.prototype.$eventHub= Vue.prototype.$eventHub || new Vue({data:{'message':''}, template:'
{{message}} '}); 31 | this.$eventHub = Vue.prototype.$eventHub; 32 | 33 | Vue.myGlobalMethod = function () { // 1. 添加全局方法或属性,如: vue-custom-element 34 | // 逻辑... 35 | console.log('我是全局的方法'); 36 | }; 37 | Vue.directive('my-directive', { // 2. 添加全局资源:指令/过滤器/过渡等,如 vue-touch 38 | bind (el, binding, vnode, oldVnode) { 39 | // 逻辑... 40 | console.log('我是全局的指令'); 41 | el.style.color = binding.value.color; 42 | el.innerHTML = binding.value.text; 43 | }, 44 | update (el, binding) { 45 | el.style.color = binding.value.color; 46 | el.innerHTML = binding.value.text; 47 | } 48 | }); 49 | Vue.mixin({ 50 | created: function () { // 3. 通过全局 mixin方法添加一些组件选项,如: vuex 51 | // 逻辑... 52 | // 合并一些东西到引入的组件内 53 | this.message = "我在封装的类内声明的变量"; 54 | } 55 | }); 56 | Vue.prototype.$myMethod = function (options) { // 4. 添加实例方法,通过把它们添加到 Vue.prototype 上实现 57 | // 逻辑... 58 | console.log('我是实例上的一个方法'); 59 | } 60 | }; 61 | // 主要用户初始化信息 62 | global.initType = function (params) { 63 | if (params == 'ios') { 64 | this.type = "ios"; 65 | } 66 | console.log(this.type); 67 | }; 68 | // 发送给原生的方法,...params : 这个参数就是一个数组 69 | global.send = function (...params) { 70 | console.log(params); 71 | }; 72 | // 注册原生发过来的方法,name需要注册的方法的名字 73 | global.registerHandler = function (name, callBack) { 74 | console.log(name + "js 处理原生发送的数据"); 75 | try { 76 | // eval(this.native.callBack) 必须指定清楚那个对象的函数名字 77 | if (typeof(eval(this.native.callBack)) == "function") { 78 | eval("this.native." + callBack + "('js处理完成之后的结果回传给原生客户端');"); 79 | } else { 80 | // 函数不存在, 抛出异常提示函数不存在 81 | alert(callBack); 82 | } 83 | }catch(err){ 84 | alert(err) // 可执行 85 | } 86 | }; 87 | global.appGoBack = function (index) { 88 | console.log("原生调用封装类" + index); 89 | //调用$emit 方法, 来发送全局的函数 90 | this.$eventHub.$emit('goBack', index); 91 | } 92 | }(window.$Hybrid); 93 | })(); 94 | --------------------------------------------------------------------------------