{"id":191035,"date":"2023-09-13T00:52:15","date_gmt":"2023-09-12T16:52:15","guid":{"rendered":"https:\/\/www.idc.net\/help\/191035\/"},"modified":"2023-09-13T00:52:15","modified_gmt":"2023-09-12T16:52:15","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91uni-app%e6%95%99%e7%a8%8b%ef%bc%9avue-%e7%bb%84%e4%bb%b6%e6%94%af%e6%8c%81","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/191035\/","title":{"rendered":"\u540e\u6d6a\u4e91UNI-APP\u6559\u7a0b\uff1aVue \u7ec4\u4ef6\u652f\u6301"},"content":{"rendered":"<h2>\u7ec4\u4ef6<\/h2>\n<h3>Vue \u7ec4\u4ef6<\/h3>\n<p>\u7ec4\u4ef6\u662f&nbsp;vue&nbsp;\u6280\u672f\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u90e8\u5206\uff0c\u7ec4\u4ef6\u4f7f\u5f97\u4e0eui\u76f8\u5173\u7684\u8f6e\u5b50\u53ef\u4ee5\u65b9\u4fbf\u7684\u5236\u9020\u548c\u5171\u4eab\uff0c\u8fdb\u800c\u4f7f\u5f97vue\u4f7f\u7528\u8005\u7684\u5f00\u53d1\u6548\u7387\u5927\u5e45\u63d0\u5347\u3002<\/p>\n<p>uni-app\u642d\u5efa\u4e86\u7ec4\u4ef6\u7684\u63d2\u4ef6\u5e02\u573a\uff0c\u53ef\u5927\u5e45\u63d0\u5347\u5f00\u53d1\u8005\u7684\u6548\u7387\u3002https:\/\/ext.dcloud.net.cn\/<\/p>\n<p>\u5728\u9879\u76ee\u7684\/component\u76ee\u5f55\u4e0b\u5b58\u653e\u7ec4\u4ef6\uff0c\u5728\u8981\u663e\u793a\u7ec4\u4ef6\u7684\u9875\u9762\u4e2d\u5219\u5206\u4e3a3\u6b65\uff1a\u5bfc\u5165\u3001\u6ce8\u518c\u548c\u4f7f\u7528\u3002<\/p>\n<p>\u53ef\u4ee5\u8fd9\u4e2a\u8bc4\u5206\u7ec4\u4ef6\u7684\u4f7f\u7528\u4e3a\u4f8b\uff0c\u4e86\u89e3vue\u7ec4\u4ef6\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<pre><code>&lt;template&gt;\n    &lt;view&gt;\n        &lt;uni-rate value=\"2\"&gt;&lt;\/uni-rate&gt; &lt;!-- \u7b2c\u4e09\u6b65\uff0c\u4f7f\u7528\u7ec4\u4ef6\u3002\u5e76\u4f20\u503c\u70b9\u4eae2\u9897\u661f --&gt;\n    &lt;\/view&gt;\n&lt;\/template&gt;\n&lt;script&gt;\nimport uniRate from \"@\/components\/uni-rate\/uni-rate.vue\" \/\/\u7b2c\u4e00\u6b65\uff0c\u5bfc\u5165\u7ec4\u4ef6\nexport default {\n    components: {\n        uniRate \/\/\u7b2c\u4e8c\u6b65\uff0c\u6ce8\u518c\u7ec4\u4ef6\n    }\n}\n&lt;\/script&gt;<\/code><\/pre>\n<ul>\n<li>2.5.0+\u7248\u672c\u652f\u6301\u5728pages.json\u5185\u5f15\u5165\u7ec4\u4ef6\uff0c\u8be6\u89c1<\/li>\n<li>uni-app\u53ea\u652f\u6301vue\u5355\u6587\u4ef6\u7ec4\u4ef6\uff08.vue \u7ec4\u4ef6\uff09\u3002\u5176\u4ed6\u7684\u8bf8\u5982\uff1a\u52a8\u6001\u7ec4\u4ef6\uff0c\u81ea\u5b9a\u4e49&nbsp;render\uff0c\u548c&lt;script type=\"text\/x-template\"&gt;&nbsp;\u5b57\u7b26\u4e32\u6a21\u7248\u7b49\uff0c\u5728\u975eH5\u7aef\u4e0d\u652f\u6301\u3002<\/li>\n<\/ul>\n<p>\u8be6\u7ec6\u7684\u975eH5\u7aef\u4e0d\u652f\u6301\u5217\u8868\uff1a<\/p>\n<ul>\n<li>Slot\uff08scoped&nbsp;\u6682\u65f6\u8fd8\u6ca1\u505a\u652f\u6301\uff09<\/li>\n<li>\u52a8\u6001\u7ec4\u4ef6<\/li>\n<li>\u5f02\u6b65\u7ec4\u4ef6<\/li>\n<li>inline-template<\/li>\n<li>X-Templates<\/li>\n<li>keep-alive<\/li>\n<li>transition&nbsp;\uff08\u53ef\u4f7f\u7528&nbsp;animation&nbsp;\u6216 CSS \u52a8\u753b\u66ff\u4ee3\uff09<\/li>\n<li>\u8001\u7684\u975e\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7f16\u8bd1\u6a21\u5f0f\u4e0d\u652f\u6301\u5728\u7ec4\u4ef6\u5f15\u7528\u65f6\uff0c\u5728\u7ec4\u4ef6\u4e0a\u5b9a\u4e49&nbsp;click&nbsp;\u7b49\u539f\u751f\u4e8b\u4ef6\u3001v-show\uff08\u53ef\u7528&nbsp;v-if&nbsp;\u4ee3\u66ff\uff09\u548c&nbsp;class&nbsp;style&nbsp;\u7b49\u6837\u5f0f\u5c5e\u6027(\u4f8b\uff1a&lt;card class=\"class-name\"&gt; &lt;\/card&gt;&nbsp;\u6837\u5f0f\u662f\u4e0d\u4f1a\u751f\u6548\u7684)\u3002\u5efa\u8bae\u66f4\u65b0\u4e3a\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u6a21\u5f0f<\/li>\n<li>\u8001\u7684\u975e\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7f16\u8bd1\u6a21\u5f0f\u7ec4\u4ef6\u91cc\u4f7f\u7528&nbsp;slot&nbsp;\u5d4c\u5957\u7684\u5176\u4ed6\u7ec4\u4ef6\u65f6\u4e0d\u652f\u6301&nbsp;v-for\u3002\u5efa\u8bae\u66f4\u65b0\u4e3a\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u6a21\u5f0f<\/li>\n<\/ul>\n<p>Vue\u5b98\u65b9\u6587\u6863\u53c2\u8003\uff1a\u7ec4\u4ef6&nbsp;\u3002<\/p>\n<h3>uni-app\u5185\u7f6e\u57fa\u7840\u7ec4\u4ef6<\/h3>\n<p>uni-app&nbsp;\u5185\u7f6e\u4e86\u5c0f\u7a0b\u5e8f\u7684\u6240\u6709\u7ec4\u4ef6\uff0c\u6bd4\u5982\uff1a&nbsp;picker,map&nbsp;\u7b49\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u539f\u751f\u7ec4\u4ef6\u4e0a\u7684\u4e8b\u4ef6\u7ed1\u5b9a\uff0c\u9700\u8981\u4ee5&nbsp;vue&nbsp;\u7684\u4e8b\u4ef6\u7ed1\u5b9a\u8bed\u6cd5\u6765\u7ed1\u5b9a\uff0c\u5982&nbsp;bindchange=\"eventName\"&nbsp;\u4e8b\u4ef6\uff0c\u9700\u8981\u5199\u6210&nbsp;@change=\"eventName\"<\/p>\n<p>\u793a\u4f8b<\/p>\n<pre><code>&lt;picker mode=\"date\" :value=\"date\" start=\"2015-09-01\" end=\"2017-09-01\" @change=\"bindDateChange\"&gt;\n    &lt;view class=\"picker\"&gt;\n      \u5f53\u524d\u9009\u62e9: {{date}}\n    &lt;\/view&gt;\n&lt;\/picker&gt;<\/code><\/pre>\n<h3>\u5168\u5c40\u7ec4\u4ef6<\/h3>\n<p>uni-app&nbsp;\u652f\u6301\u914d\u7f6e\u5168\u5c40\u7ec4\u4ef6\uff0c\u9700\u5728&nbsp;main.js&nbsp;\u91cc\u8fdb\u884c\u5168\u5c40\u6ce8\u518c\uff0c\u6ce8\u518c\u540e\u5c31\u53ef\u5728\u6240\u6709\u9875\u9762\u91cc\u4f7f\u7528\u8be5\u7ec4\u4ef6\u3002<\/p>\n<p>\u6ce8\u610f<\/p>\n<ul>\n<li>Vue.component&nbsp;\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f\u9759\u6001\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>nvue\u9875\u9762\u6682\u4e0d\u652f\u6301\u5168\u5c40\u7ec4\u4ef6<\/li>\n<\/ul>\n<p>\u793a\u4f8b<\/p>\n<p>main.js \u91cc\u8fdb\u884c\u5168\u5c40\u5bfc\u5165\u548c\u6ce8\u518c<\/p>\n<pre><code>import Vue from 'vue'\nimport pageHead from '.\/components\/page-head.vue'\nVue.component('page-head',pageHead)<\/code><\/pre>\n<p>index.vue \u91cc\u53ef\u76f4\u63a5\u4f7f\u7528\u7ec4\u4ef6<\/p>\n<pre><code>&lt;template&gt;\n  &lt;view&gt;\n    &lt;page-head&gt;&lt;\/page-head&gt;\n    &lt;\/view&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<h3>\u547d\u540d\u9650\u5236<\/h3>\n<p>\u5728&nbsp;uni-app&nbsp;\u4e2d\u4ee5\u4e0b\u8fd9\u4e9b\u4f5c\u4e3a\u4fdd\u7559\u5173\u952e\u5b57\uff0c\u4e0d\u53ef\u4f5c\u4e3a\u7ec4\u4ef6\u540d\u3002<\/p>\n<ul>\n<li>a<\/li>\n<li>canvas<\/li>\n<li>cell<\/li>\n<li>content<\/li>\n<li>countdown<\/li>\n<li>datepicker<\/li>\n<li>div<\/li>\n<li>element<\/li>\n<li>embed<\/li>\n<li>header<\/li>\n<li>image<\/li>\n<li>img<\/li>\n<li>indicator<\/li>\n<li>input<\/li>\n<li>link<\/li>\n<li>list<\/li>\n<li>loading-indicator<\/li>\n<li>loading<\/li>\n<li>marquee<\/li>\n<li>meta<\/li>\n<li>refresh<\/li>\n<li>richtext<\/li>\n<li>script<\/li>\n<li>scrollable<\/li>\n<li>scroller<\/li>\n<li>select<\/li>\n<li>slider-neighbor<\/li>\n<li>slider<\/li>\n<li>slot<\/li>\n<li>span<\/li>\n<li>spinner<\/li>\n<li>style<\/li>\n<li>svg<\/li>\n<li>switch<\/li>\n<li>tabbar<\/li>\n<li>tabheader<\/li>\n<li>template<\/li>\n<li>text<\/li>\n<li>textarea<\/li>\n<li>timepicker<\/li>\n<li>transition-group<\/li>\n<li>transition<\/li>\n<li>video<\/li>\n<li>view<\/li>\n<li>web<\/li>\n<\/ul>\n<p>Tips<\/p>\n<ul>\n<li>\u9664\u4ee5\u4e0a\u5217\u8868\u4e2d\u7684\u540d\u79f0\u5916\uff0c\u6807\u51c6\u7684 HTML \u53ca SVG \u6807\u7b7e\u540d\u4e5f\u4e0d\u80fd\u4f5c\u4e3a\u7ec4\u4ef6\u540d\u3002<\/li>\n<li>\u5728\u767e\u5ea6\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u65f6\uff0c\u4e0d\u8981\u5728 data \u5185\u4f7f\u7528 hidden \uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6e32\u67d3\u9519\u8bef<\/li>\n<\/ul>\n<h2>\u5e38\u89c1\u95ee\u9898<\/h2>\n<p>1. \u5982\u4f55\u83b7\u53d6\u4e0a\u4e2a\u9875\u9762\u4f20\u9012\u7684\u6570\u636e<\/p>\n<p>\u5728&nbsp;onLoad&nbsp;\u91cc\u5f97\u5230\uff0conLoad&nbsp;\u7684\u53c2\u6570\u662f\u5176\u4ed6\u9875\u9762\u6253\u5f00\u5f53\u524d\u9875\u9762\u6240\u4f20\u9012\u7684\u6570\u636e\u3002<\/p>\n<p>2. \u5982\u4f55\u8bbe\u7f6e\u5168\u5c40\u7684\u6570\u636e\u548c\u5168\u5c40\u7684\u65b9\u6cd5<\/p>\n<p>uni-app&nbsp;\u5185\u7f6e\u4e86&nbsp;vuex&nbsp;\uff0c\u5728app\u91cc\u7684\u4f7f\u7528\uff0c\u53ef\u53c2\u8003hello-uniapp&nbsp;store\/index.js\u3002<\/p>\n<pre><code>\/\/store.js\nimport Vue from 'vue'\nimport Vuex from 'vuex'\nVue.use(Vuex)\nconst store = new Vuex.Store({\n    state: {...},\n    mutations: {...},\n    actions: {...}\n})\n\nexport default store\n\n\/\/main.js\n...\nimport store from '.\/store'\nVue.prototype.$store = store\nconst app = new Vue({\n    store,...\n})\n...\n\n\/\/test.vue \u4f7f\u7528\u65f6\uff1a\nimport {mapState,mapMutations} from 'vuex'<\/code><\/pre>\n<p>3. \u5982\u4f55\u6355\u83b7 app \u7684 onError<\/p>\n<p>\u7531\u4e8e&nbsp;onError&nbsp;\u5e76\u4e0d\u662f\u5b8c\u6574\u610f\u4e49\u7684\u751f\u547d\u5468\u671f\uff0c\u6240\u4ee5\u53ea\u63d0\u4f9b\u4e00\u4e2a\u6355\u83b7\u9519\u8bef\u7684\u65b9\u6cd5\uff0c\u5728&nbsp;app&nbsp;\u7684\u6839\u7ec4\u4ef6\u4e0a\u6dfb\u52a0\u540d\u4e3a&nbsp;onError&nbsp;\u7684\u56de\u8c03\u51fd\u6570\u5373\u53ef\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code>export default {\n   \/\/ \u53ea\u6709 app \u624d\u4f1a\u6709 onLaunch \u7684\u751f\u547d\u5468\u671f\n   onLaunch () {\n       \/\/ ...\n   },\n\n   \/\/ \u6355\u83b7 app error\n   onError (err) {\n       console.log(err)\n   }\n}<\/code><\/pre>\n<p>4. \u7ec4\u4ef6\u5c5e\u6027\u8bbe\u7f6e\u4e0d\u751f\u6548\u89e3\u51b3\u529e\u6cd5<\/p>\n<p>\u5f53\u91cd\u590d\u8bbe\u7f6e\u67d0\u4e9b\u5c5e\u6027\u4e3a\u76f8\u540c\u7684\u503c\u65f6\uff0c\u4e0d\u4f1a\u540c\u6b65\u5230view\u5c42\u3002 \u4f8b\u5982\uff1a\u6bcf\u6b21\u5c06scroll-view\u7ec4\u4ef6\u7684scroll-top\u5c5e\u6027\u503c\u8bbe\u7f6e\u4e3a0\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u80fd\u987a\u5229\u8fd4\u56de\u9876\u90e8\u3002 \u8fd9\u548cprops\u7684\u5355\u5411\u6570\u636e\u6d41\u7279\u6027\u6709\u5173\uff0c\u7ec4\u4ef6\u5185\u90e8scroll-top\u7684\u5b9e\u9645\u503c\u6539\u52a8\u540e\uff0c\u5176\u7ed1\u5b9a\u7684\u5c5e\u6027\u5e76\u4e0d\u4f1a\u4e00\u540c\u53d8\u5316\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\u6709\u4e24\u79cd\uff08\u4ee5scroll-view\u7ec4\u4ef6\u4e3a\u4f8b\uff09\uff1a<\/p>\n<ul>\n<li>\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u8bb0\u5f55\u7ec4\u4ef6\u5185\u90e8\u53d8\u5316\u7684\u503c\uff0c\u5728\u8bbe\u7f6e\u65b0\u503c\u4e4b\u524d\u5148\u8bbe\u7f6e\u4e3a\u8bb0\u5f55\u7684\u5f53\u524d\u503c<\/li>\n<\/ul>\n<pre><code>&lt;scroll-view :scroll-top=\"scrollTop\" scroll-y=\"true\" @scroll=\"scroll\"&gt;<\/code><\/pre>\n<pre><code>export default {\n    data() {\n        return {\n            scrollTop: 0,\n            old: {\n                scrollTop: 0\n            }\n        }\n    },\n    methods: {\n        scroll: function(e) {\n            this.old.scrollTop = e.detail.scrollTop\n        },\n        goTop: function(e) {\n            this.scrollTop = this.old.scrollTop\n            this.$nextTick(function() {\n                this.scrollTop = 0\n            });\n        }\n    }\n}<\/code><\/pre>\n<ul>\n<li>\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u83b7\u53d6\u7ec4\u4ef6\u5185\u90e8\u53d8\u5316\u7684\u503c\uff0c\u5b9e\u65f6\u66f4\u65b0\u5176\u7ed1\u5b9a\u503c<\/li>\n<\/ul>\n<pre><code>&lt;scroll-view :scroll-top=\"scrollTop\" scroll-y=\"true\" @scroll=\"scroll\"&gt;<\/code><\/pre>\n<pre><code>export default {\n    data() {\n        return {\n            scrollTop: 0,\n        }\n    },\n    methods: {\n        scroll: function(e) {\n            this.scrollTop = e.detail.scrollTop\n        },\n        goTop: function(e) {\n            this.scrollTop = 0\n        }\n    }\n}<\/code><\/pre>\n<p>\u7b2c\u4e8c\u79cd\u89e3\u51b3\u65b9\u5f0f\u5728\u67d0\u4e9b\u7ec4\u4ef6\u53ef\u80fd\u9020\u6210\u6296\u52a8\uff0c\u63a8\u8350\u7b2c\u4e00\u79cd\u89e3\u51b3\u65b9\u5f0f\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6 Vue \u7ec4\u4ef6 \u7ec4\u4ef6\u662f&nbsp;vue&nbsp;\u6280\u672f\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u90e8\u5206\uff0c\u7ec4\u4ef6\u4f7f\u5f97\u4e0eui\u76f8\u5173\u7684\u8f6e\u5b50\u53ef\u4ee5\u65b9\u4fbf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-191035","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=191035"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191035\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=191035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=191035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=191035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}