weex 项目开发(五)自定义 过滤函数 和 混合 及 自定义 Header 组件
1.自定义 过滤函数
src / filters / index.js
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 | /** * 自定义 过滤函数 */ export function host (url) { if (!url) return '' const host = url.replace(/^https?:\/\ //, '').replace(/\/.*$/, '') const parts = host.split( '.' ).slice(-3) if (parts[0] === 'www' ) parts.shift() return parts.join( '.' ) } export function https (url) { const env = weex.config.env || WXEnvironment if (env.platform === 'iOS' && typeof url === 'string' ) { return url.replace(/^http\:/, 'https:' ) } return url } export function timeAgo (time) { const between = Date.now() / 1000 - Number(time) if (between < 3600) { return pluralize(~~(between / 60), ' minute' ) } else if (between < 86400) { return pluralize(~~(between / 3600), ' hour' ) } else { return pluralize(~~(between / 86400), ' day' ) } } function pluralize (time, label) { if (time === 1) { return time + label } return time + label + 's' } export function unescape (text) { let res = text || '' ;[ [ '<p>' , '\n' ], [ '&' , '&' ], [ '&' , '&' ], [ '' ', ' \ '' ], [ '' ', ' \ '' ], [ '/' , '/' ], [ '' ', ' \ '' ], [ '/' , '/' ], [ '<' , '<' ], [ '>' , '>' ], [ ' ' , ' ' ], [ '"' , '"' ] ].forEach(pair => { res = res.replace( new RegExp(pair[0], 'ig' ), pair[1]) }) return res } |
2.自定义 混合 函数
src / mixins / index.js
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 混合 */ export default { methods: { jump (to) { if ( this .$router) { this .$router.push(to) } } } } |
3.自定义 Header 组件
src / components / Header.vue
Header.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 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 | <!-- Header 组件 --> <template> <div class = "wrapper" > <div class = "scan" > <text class = "ic iconfont" ></text> <text class = "txt" >扫一扫</text> </div> <text class = "search iconfont" @click= "jumpWeb()" > 搜索商品,共8888款好物</text> <div class = "notice" > <text class = "ic iconfont" ></text> <text class = "txt" >消息</text> </div> </div> </template> <script> var navigator = weex.requireModule( 'navigator' ); import util from '../utils/util.js' ; export default { data () { return { // } }, created () { // }, methods: { jumpWeb (_url) { if (!_url) _url = 'http://m.you.163.com/search' ; const url = this .$getConfig().bundleUrl; navigator.push({ url: util.setBundleUrl(url, 'page/webview.js?weburl=' +_url) , animated: "false" }); } } } </script> <style scoped> .iconfont { font-family:iconfont; } .wrapper{ position: fixed; top: 0; left: 0;right: 0; height: 114px; padding-top: 34px; display:flex; flex-wrap: nowrap; flex-direction: row; justify-content: space-around; z-index: 101; background-color: #fafafa; opacity: .99; } .scan,.notice{ height: 80px; width: 96px } .ic,.txt,.search{ text-align: center; color: #666; font-weight: 300; } .ic{ font-size: 32px; } .txt{ font-size: 18px; } .search { flex: 1; height: 60px; font-size: 26px; padding-top: 13px; background-color: #ededed; border-radius: 8px; } </style> |
4.页面调用
src / pages / Home / Home.vue
Home.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 | <!-- 首页 --> <template> <div> <!-- 顶部标题栏 --> <home-header></home-header> </div> </template> <script> import Header from '../../components/Header.vue' ; export default { name: 'Home' , components: { 'home-header' : Header, }, data: () => ({ // }), created () { // }, methods: { // } }; </script> |
5.效果图
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步