如何实现页面间数据共享

本示例针对有独立页面间数据共享需求的开发者

完整示例代码

其实执行到上一步我们已经完成了示例的全部开发步骤。但为了清晰和可读性考虑,我们把最终形态的示例代码展示给开发者。

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<html> <head> <meta charset="utf-8"> <title>首页</title> <meta name="apple-touch-fullscreen" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" href="https://c.mipcdn.com/static/v2/mip.css"> <style mip-custom> h1 { text-align: center; line-height: 80px; } .city { text-align: center; line-height: 40px; } .next-link { display: block; margin: 0 auto; width: 170px; text-align: center; background: #69f; border-radius: 20px; height: 40px; line-height: 40px; color: white; } </style> </head> <body> <mip-shell> <script type="application/json"> { "routes": [ { "pattern": "/index.html", "meta": { "header": { "show": true, "title": "我的首页", "buttonGroup": [ { "name": "subscribe", "text": "关注" }, { "name": "chat", "text": "发消息" } ] }, "view": { "isIndex": true } } }, { "pattern": "/city-selector.html", "meta": { "header": { "show": true, "title": "城市选择页" } } } ] } </script> </mip-shell> <mip-data> <script type="application/json"> { "#city": "上海", "title": "首页标题" } </script> </mip-data> <h1 m-text="title"></h1> <p class="city">选择城市:<span m-text="city"></span></p> <a mip-link href="./city-selector.html" class="next-link">Go to City Selector Page</a> <script src="https://c.mipcdn.com/static/v2/mip.js"></script> </body> </html>

city-selector.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<html> <head> <meta charset="utf-8"> <title>城市选择页</title> <meta name="apple-touch-fullscreen" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" href="https://c.mipcdn.com/static/v2/mip.css"> <style mip-custom> h1 { text-align: center; line-height: 80px; } .city { padding: 20px; background: rgba(0, 0, 0, .1) } .city span { color: red; } </style> </head> <body> <mip-shell> <script type="application/json"> { "routes": [ { "pattern": "/index.html", "meta": { "header": { "show": true, "title": "我的首页", "buttonGroup": [ { "name": "subscribe", "text": "关注" }, { "name": "chat", "text": "发消息" } ] }, "view": { "isIndex": true } } }, { "pattern": "/city-selector.html", "meta": { "header": { "show": true, "title": "城市选择页" } } } ] } </script> </mip-shell> <mip-data> <script type="application/json"> { "title": "城市选择页标题" } </script> </mip-data> <h1 m-text="title"></h1> <p class="city">当前选择城市: <span m-text="city"></span></p> <mip-city-list> <script type="application/json"> { "list": [ "北京", "上海", "广州", "深圳", "成都", "武汉", "拉萨", "南昌", "杭州", "苏州", "南京", "天津" ] } </script> </mip-city-list> <script src="https://c.mipcdn.com/static/v2/mip.js"></script> <script src="http://somecdn.com/mip-city-list.js"></script> </body> </html>

mip-city-list.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<template> <div class="mip-city-list-wrapper"> <p class="sub-header">城市列表:</p> <ul> <li v-for="(item, i) in list" @click="select(i)">{{item}}</li> </ul> </div> </template> <script> export default { props: { list: { default: () => [], type: Array } }, methods: { select (index) { MIP.setData({ city: this.list[index] }) } } } <script> <style scoped> .mip-city-list-wrapper { padding: 20px; } .mip-city-list-wrapper ul { list-style-type: none; line-height: 28px; margin-left: 20px; } </style>