Skip to content

Commit eaea733

Browse files
committed
vue 3 Refactor
1 parent beba92f commit eaea733

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4728
-7745
lines changed

.browserslistrc

-2
This file was deleted.

.env.dev

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
NODE_ENV = 'production'
2-
VUE_APP_MODE = 'buildDev'
1+
VUE_APP_ENV='Dev'
2+
VUE_APP_REQ_URL='https://xusenlin.com'
3+

.env.devtest

-2
This file was deleted.

.env.pre

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VUE_APP_ENV='Pre'
2+
VUE_APP_REQ_URL='http://localhost:8080'
3+

.env.pro

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VUE_APP_ENV='Pro'
2+
VUE_APP_REQ_URL='http://localhost:8080'

.env.test

-2
This file was deleted.

.eslintrc.js

-14
This file was deleted.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
node_modules
33
/dist
44

5+
56
# local env files
67
.env.local
78
.env.*.local
@@ -10,6 +11,7 @@ node_modules
1011
npm-debug.log*
1112
yarn-debug.log*
1213
yarn-error.log*
14+
pnpm-debug.log*
1315

1416
# Editor directories and files
1517
.idea

README.md

+26-40
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,43 @@
22

33
## 一个 Vue 多页面应用,适用于移动端不需要单页应用(SPA)的场景
44

5-
### Project setup
6-
7-
```
8-
yarn install
9-
```
10-
11-
### Compiles and hot-reloads for development
12-
13-
```
14-
yarn dev
15-
```
16-
17-
### Compiles and minifies
185

196
```
207
yarn build:dev //打包开发环境
21-
yarn build:devtest //打包开发测试环境
22-
yarn build:test //打包测试环境
8+
yarn build:pre //打包测试环境
239
yarn build // 打包正式环境
2410
```
2511

26-
### Lints and fixes files
27-
28-
```
29-
yarn lint
30-
```
12+
> 番外: [MareWood](https://github.com/xusenlin/MareWood) 是一个 Go 开发的轻量级前端部署工具,可以很灵活的配置各种打包部署环境并提供访问,特别是远程的时候,方便后端和测试使用,草鸡好用。
3113
32-
### Customize configuration
14+
本人做了很多H5相关的项目,如 M站、微信活动页面、原生App内嵌H5页面(jsBridge交互),从实践过程中,真的不建议大家在这部分做单页应用(SPA),首先微信里面的H5页面
15+
在安卓和ios的url机制不一样,因此会导致两边的微信签名需要针对不同平台处理,当项目越来越大的时候,从其他应用跳转到这个应用的时候,即使你只使用其中一个页面,但是可能需要你去处理路由
16+
和权限,页面的复用性没有多页面的好。使用多页面的话每个页面之间没有多余的联系,我可以配置好每个页面的标题,每个页面的url必须携带的参数等,页面之间的跳转可以直接
17+
`window.location.href = `也可以通过 jsBridge(在原生app的环境)去跳转。
3318

34-
See [Configuration Reference](https://cli.vuejs.org/config/).
19+
# 更新日志
3520

36-
这是一个做了大量移动端的 vue 多页面最佳实践(自我认为)。使用了全新依赖和 ESLint+Prettier 第二次重构(2020.01)。
21+
### 第三次重构(2022.08)最新vueCli v5.0.8 + Vue(<script setup>) v3.2 + vant v4.0
3722

38-
### 说明
23+
在后台重构的时候我选择了vite,虽然vite现阶段带来了很棒的开发体验,但是它的多页面配置在开发阶段和打包上线阶段的不一致会
24+
导致页面跳转得不完美,即使目前有很多插件去支持多页面,但是都不是很完美,因此我选择继续使用vuecli,如果有一天vite有了更好的解决方式,
25+
我会第一时间重构到 vite + ts。
3926

40-
- 使用 normalize.css 重置样式。(如果使用 vant 可以去掉)
4127
- 添加了 axios 请求库,并做了简单的拦截。
4228
- 添加了漂亮的移动端调试工具 eruda,方便在手机上调试(使用 npm run build 命令不会出现此工具)。
43-
- 添加了 Url 参数验证和初始化,`Vue.prototype.$pageParams` 保存了 Url 携带的参数对象。`let { id } = this.$pageParams;`
44-
- `window.PAGE_PATH(Vue.prototype.$pageName)` 表示当前页面的名字,如 index 目录生成 index.html,window.PAGE_PATH 就是 index
45-
- 想要添加自己 UI 库,安装好在 common.js 引用即可。(推荐 Vant)
46-
- 封装了微信 jsSdk 的常用签名分享等一堆堆常用的东西,食用前请先安装 weixin-js-sdk,并在 utils/wechat.js 引入
47-
- 添加 postcss-px2rem 自动将 px 转换为 rem 适配移动端,目前为了和大部分 ui 库兼容,设置的设计稿宽度为 375,可自行修改。
48-
- 添加了 node 工具,在 src/utils/nodeTool 下,运行 node index.js -h 即可查看使用方法。
49-
- 添加页面请在 pages 文件夹下新建目录,在里面放置 index.js 和 Index.vue(建议使用提供的 node 工具生成页面,他会更新你的配置)。编译后,目录的名字即为网页的名字。至于为什么?请查看 page.config.js
50-
- 如果页面太多,可以通过在 pages 下添加分组目录来分类页面,支持无限层级目录,只要目录里存在 index.js 和 Index.vue 即被认为是一个页面。
51-
- 没有路由(vue-route),页面跳转请使用
52-
53-
```javascript
54-
window.location.href = "./demo.html" + obj2StrParams(params);
55-
```
29+
- 修改了 Url 参数验证和初始化,`window.$pageParams` 保存了 Url 携带的参数对象。`let { id } = window.$pageParams;`
30+
- `window.$pageName` 表示当前页面的名字,如 demo 目录生成 demo.html,window.$pageName 就是 demo
31+
- 支持pages目录无限嵌套,只要一个目录里面包含`main.js``App.js`,就会认为当前目录是一个html页面。因此可以合理的通过目录来分类你的html页面。
32+
- 封装了微信 jsSdk 的常用签名分享等一堆堆常用的东西,食用前请先安装 weixin-js-sdk并引入
33+
- 将postcss-px2rem重构为postcss-px-to-viewpor自动将px单位转换vw,更好的适配方案,目前为了和大部分 ui 库兼容,设置的设计稿宽度为 375,可自行修改。
34+
- 没有路由(vue-route),页面跳转请使用`window.location.href = "./demo.html" + obj2StrParams(params);`
35+
- 所有的页面默认使用public/index.html作为模板,除非你在页面目录下添加index.html
36+
- 每次添加页面目录需要重新运行 npm run dev
37+
38+
39+
### 第二次重构(2020.01) 全新依赖和ESLint+Prettier重构 v0.0.1
40+
此版本的说明请切换分支版本查看
41+
5642

5743
### 其他
5844

@@ -65,11 +51,11 @@ window.location.href = "./demo.html" + obj2StrParams(params);
6551
<meta name="viewport" content="width=device-width" />
6652
```
6753

68-
浏览器就会以这种方式假定您已使文本在移动设备上可读,因此无需双击
54+
浏览器就会以这种方式假定您已使文本在移动设备上可读,因此无需fastclick
6955

7056
---
7157

7258
还有各种移动端奇形怪状的问题解决方案
7359
https://juejin.im/post/5d6e1899e51d453b1e478b29
7460

75-
> 番外: [MareWood](https://github.com/xusenlin/MareWood) 是一个 Go 开发的轻量级前端部署工具,可以很灵活的配置各种打包部署环境并提供访问,特别是远程的时候,方便后端和测试使用,草鸡好用。
61+

babel.config.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
presets: ["@vue/cli-plugin-babel/preset"]
3-
};
2+
presets: [
3+
'@vue/cli-plugin-babel/preset'
4+
]
5+
}

jsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "esnext",
5+
"baseUrl": "./",
6+
"moduleResolution": "node",
7+
"paths": {
8+
"@/*": [
9+
"src/*"
10+
]
11+
},
12+
"lib": [
13+
"esnext",
14+
"dom",
15+
"dom.iterable",
16+
"scripthost"
17+
]
18+
}
19+
}

package.json

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
{
2-
"name": "vuemultiplepages",
2+
"name": "vue-multiple-pages",
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"dev": "vue-cli-service serve",
7-
"build": "vue-cli-service build",
8-
"build:dev": "vue-cli-service build --mode dev",
9-
"build:devtest": "vue-cli-service build --mode devtest",
10-
"build:test": "vue-cli-service build --mode test",
6+
"dev": "vue-cli-service serve --mode dev",
7+
"build": "vue-cli-service build --mode pro",
8+
"build:pre": "vue-cli-service build --mode pre",
119
"lint": "vue-cli-service lint"
1210
},
1311
"dependencies": {
14-
"axios": "^0.21.1",
15-
"core-js": "^3.4.4",
16-
"good-storage": "^1.1.0",
17-
"normalize.css": "^8.0.1",
18-
"vue": "^2.6.10"
12+
"axios": "^0.27.2",
13+
"good-storage": "^1.1.1",
14+
"vant": "^4.0.0-beta.0",
15+
"vue": "^3.2.13"
1916
},
2017
"devDependencies": {
21-
"@vue/cli-plugin-babel": "^4.1.0",
22-
"@vue/cli-plugin-eslint": "^4.1.0",
23-
"@vue/cli-service": "^4.1.0",
24-
"@vue/eslint-config-prettier": "^5.0.0",
25-
"babel-eslint": "^10.0.3",
26-
"eslint": "^5.16.0",
27-
"eslint-plugin-prettier": "^3.1.1",
28-
"eslint-plugin-vue": "^5.0.0",
29-
"node-sass": "^4.13.1",
30-
"postcss-px2rem": "^0.3.0",
31-
"prettier": "^1.19.1",
32-
"sass-loader": "^8.0.0",
33-
"vue-template-compiler": "^2.6.10"
34-
}
18+
"@babel/core": "^7.12.16",
19+
"@babel/eslint-parser": "^7.12.16",
20+
"@vue/cli-plugin-babel": "~5.0.0",
21+
"@vue/cli-plugin-eslint": "~5.0.0",
22+
"@vue/cli-service": "~5.0.0",
23+
"core-js": "^3.8.3",
24+
"eslint": "^7.32.0",
25+
"eslint-plugin-vue": "^8.0.3",
26+
"postcss-px-to-viewport": "^1.1.1",
27+
"sass": "^1.54.5",
28+
"sass-loader": "^13.0.2"
29+
},
30+
"eslintConfig": {
31+
"root": true,
32+
"env": {
33+
"node": true,
34+
"vue/setup-compiler-macros": true
35+
},
36+
"extends": [
37+
"plugin:vue/vue3-essential",
38+
"eslint:recommended"
39+
],
40+
"rules": {}
41+
},
42+
"browserslist": [
43+
"> 1%",
44+
"last 2 versions",
45+
"not dead",
46+
"not ie 11"
47+
]
3548
}

page.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let pageConfig = {};
55

66
function addPageConfig(path, dir) {
77
if (isPage(path + "/" + dir)) {
8-
if (pageConfig.hasOwnProperty(dir)) {
8+
if (pageConfig.dir) {
99
throw new Error("有名字重复的页面:" + dir);
1010
}
1111
let template = "public/index.html";
@@ -14,10 +14,10 @@ function addPageConfig(path, dir) {
1414
template = path + "/" + dir + "/index.html";
1515
}
1616
pageConfig[dir] = {
17-
entry: path + "/" + dir + "/index.js",
17+
entry: path + "/" + dir + "/main.js",
1818
filename: dir + ".html",
1919
path: dir,
20-
title: Config.hasOwnProperty(dir) ? Config[dir].title : "",
20+
title: Config.dir ? Config[dir].title : "",
2121
template: template
2222
};
2323
}
@@ -31,7 +31,7 @@ function addPageConfig(path, dir) {
3131
}
3232

3333
function isPage(dir) {
34-
return fs.existsSync(dir + "/index.js") && fs.existsSync(dir + "/Index.vue");
34+
return fs.existsSync(dir + "/main.js") && fs.existsSync(dir + "/App.vue");
3535
}
3636

3737
addPageConfig("src", "pages");

public/index.html

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
<!DOCTYPE html>
22
<html lang="zh-cn">
3-
<head>
4-
<meta charset="utf-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6-
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport" />
7-
<meta name="format-detection" content="telephone=yes" />
8-
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
9-
<title><%= htmlWebpackPlugin.options.title %></title>
10-
<% if(!("production" == process.env.NODE_ENV && undefined == process.env.VUE_APP_MODE)){ %>
11-
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
12-
<script>eruda.init();</script>
13-
<% } %>
14-
<script>
15-
window.PAGE_PATH = '<%= htmlWebpackPlugin.options.path %>';
16-
</script>
17-
</head>
18-
<body>
19-
<noscript>
20-
<strong>We're sorry but vueMultiplePages doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
21-
</noscript>
22-
<div id="app"></div>
23-
<!-- built files will be auto injected -->
24-
</body>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport"/>
7+
<meta name="format-detection" content="telephone=yes"/>
8+
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
9+
<title><%= htmlWebpackPlugin.options.title %></title>
10+
<% if("Pro" != process.env.VUE_APP_ENV){ %>
11+
<script src="https://cdn.bootcdn.net/ajax/libs/eruda/2.5.0/eruda.min.js"></script>
12+
<script>eruda.init({tool: ['console','network','info','resource']});</script>
13+
<% } %>
14+
<script>
15+
window.PAGE_PATH = '<%= htmlWebpackPlugin.options.path %>';
16+
</script>
17+
</head>
18+
<body>
19+
<noscript>
20+
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
21+
Please enable it to continue.</strong>
22+
</noscript>
23+
<div id="app"></div>
24+
<!-- built files will be auto injected -->
25+
</body>
2526
</html>

src/api/wechat/api.js

-27
This file was deleted.

src/api/wechat/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
export function wechatSignatureApi(){
4+
5+
}

0 commit comments

Comments
 (0)