段莲 發表於 2019-9-29 21:05:00

uni-app学习资料整理-1.白话uni-app

<h2>白话uni-app&nbsp;&nbsp;https://ask.dcloud.net.cn/article/35657</h2>
<h3>文件内代码架构的变化</h3>
<p>以前一个html大节点,里面有script和style节点;</p>
<p>现在template是一级节点,用于写tag组件,script和style是并列的一级节点,也就是有3个一级节点。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">    注意必须有一个view,且只能有一个根view。所有内容写在这个view下面。
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    export </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">default</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3>外部文件引用方式变化</h3>
<p>以前通过script src、link href引入外部的js和css;</p>
<p>现在是es6的写法,import引入外部的js模块(注意不是文件)或css</p>
<h4>js要require进来,变成了对象。</h4>
<p>在hello uni-app的common目录有一个工具类<code>util.js</code>,可以在hello uni-app中搜索这个例子查看。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">var</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> util </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> require(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">'</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">../../../common/util.js</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">'</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">);</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">require这个js模块</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">var</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> formatedPlayTime </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> util.formatTime(playTime); </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">调用js模块的方法</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>而在这个<code>util.js</code>里,要把之前的function封装为对象的方法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">function formatTime(time) {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">    return time;//这里没写逻辑
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)">module.exports = {
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)">    formatTime: formatTime
</span><span style="color: rgba(0, 128, 128, 1)">7</span> }</pre>
</div>
<p>当然还有一些高级的用法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">var dateUtils = require('../../../common/util.js').dateUtils; //直接使用js模块的属性。在hello uni-app有示例
</span><span style="color: rgba(0, 128, 128, 1)">2</span> import * as echarts from '/components/echarts/echarts.simple.min.js'; //将js导入并重命名为echarts,然后使用echarts.来继续执行方法。在hello uni-app有示例</pre>
</div>
<h4>css外部文件导入</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(128, 0, 0, 1)">    @import "./common/uni.css";
</span><span style="color: rgba(0, 128, 128, 1)">3</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(128, 0, 0, 1)">    .uni-hello-text</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">{</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(255, 0, 0, 1)">      color</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">:</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">#7A7E83</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">;</span>
<span style="color: rgba(0, 128, 128, 1)">6</span>   <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>全局样式,在根目录下的app.vue里写入,每个页面都会加载app.vue里的样式。</p>
<h4>另外,vue支持组件导入,可以更方便的封装一个包括界面、js、样式的库。</h4>
<p>如下是导入一个角标的组件库,在页面上显示一个abc并且右上角有个数字角标1,详见</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uni-badge </span><span style="color: rgba(255, 0, 0, 1)">text</span><span style="color: rgba(0, 0, 255, 1)">="abc"</span><span style="color: rgba(255, 0, 0, 1)"> :inverted</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">uni-badge</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">3.使用组件</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    import uniBadge from </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">../../../components/uni-badge.vue</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">;</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">1.导入组件</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    export </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">default</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      data() {
</span><span style="color: rgba(0, 128, 128, 1)">10</span>             <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">return</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">11</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      },
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      components: {
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">            uniBadge </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">2.注册组件</span>
<span style="color: rgba(0, 128, 128, 1)">16</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>如需要全局导入vue组件,即每个页面都可以直接使用而不用引用和注册的话,在项目根目录下的main.js里处理。如下是hello uni-app里的例子。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">//main.js
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">import pageHead from './components/page-head.vue' //导入
</span><span style="color: rgba(0, 128, 128, 1)">3</span> Vue.component('page-head', pageHead) //注册。注册后在每个vue的page页面里可以直接使用<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">page-head</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">page-head</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>组件。</pre>
</div>
<h3>组件/标签的变化</h3>
<p>以前是html标签,现在是小程序组件。<br>具体说来:</p>
<ul>
<ul>
<li>div 改成&nbsp;view</li>
<li>span、font 改成&nbsp;text</li>
<li>a 改成&nbsp;navigator</li>
<li>img 改成&nbsp;image</li>
<li>input&nbsp;还在,但type属性改成了confirmtype</li>
<li>form、button、checkbox、radio、label、textarea、canvas、video&nbsp;这些还在。</li>
<li>select 改成&nbsp;picker</li>
<li>iframe 改成&nbsp;web-view</li>
<li>ul、li没有了,都用view替代</li>
<li>audio 不再推荐使用,改成api方式,背景音频api文档<br>其实老的HTML标签也可以在uni-app里使用,uni-app编译器会在编译时把老标签转为新标签,比如把div编译成view。但不推荐这种用法,调试H5端时容易混乱,基于老元素写法的选择器也会出问题。</li>


</ul>


</ul>
<p>除了改动外,新增了一批手机端常用的新组件</p>
<ul>
<li>scroll-view&nbsp;可区域滚动视图容器</li>
<li>swiper&nbsp;可滑动区域视图容器</li>
<li>icon&nbsp;图标</li>
<li>rich-text&nbsp;富文本(不可执行js,但可渲染各种文字格式和图片)</li>
<li>progress&nbsp;进度条</li>
<li>slider&nbsp;滑块指示器</li>
<li>switch&nbsp;开关选择器</li>
<li>camera&nbsp;相机</li>
<li>live-player&nbsp;直播</li>
<li>map&nbsp;地图</li>
<li>
<p>cover-view&nbsp;可覆盖原生组件的视图容器&nbsp;</p>
<p>cover-view需要多强调几句,uni-app的非h5端的video、map、canvas、textarea是原生组件,层级高于其他组件。如需覆盖原生组件,比如在map上加个遮罩,则需要使用cover-view组件</p>


</li>


</ul>
<p>除了内置组件,还有很多开源的扩展组件,把常用操作都进行封装,DCloud建立了插件市场收录这些扩展组件,详见插件市场</p>
<h3>js的变化</h3>
<p>js的变化,分为运行环境变化、数据绑定模式变化、api变化3部分。</p>
<ul>
<li>运行环境从浏览器变成v8引擎</li>


</ul>
<p>标准js语法和api都支持,比如if、for、settimeout、indexOf等。</p>
<p>但浏览器专用的window、document、navigator、location对象,包括cookie等存储,只有在浏览器中才有,app和小程序都不支持。</p>
<p>可能有些人以为js等于浏览器里的js。其实js是ECMAScript组织管理的,浏览器中的js是w3c组织基于js规范补充了window、document、navigator、location等专用对象。</p>
<p>在uni-app的各个端中,除了h5端,其他端的js都运行在一个独立的v8引擎下,不是在浏览器中,所以浏览器的对象无法使用。如果你做过小程序开发,对此应当很了解。</p>
<p>这意味着依赖document的很多HTML的库,比如jqurey无法使用。</p>
<p>当然app和小程序支持web-view组件,里面可以加载标准HTML,这种页面仍然支持浏览器专用对象window、document、navigator、location。</p>
<ul>
<li>以前的dom操作,改成vue的MVVM模式</li>


</ul>
<p>现在前端趋势是去dom化,改用mvvm模式,更简洁的写法,大幅减少代码行数,同时差量渲染性能更好。</p>
<p>uni-app使用vue的数据绑定方式解决js和dom界面交互的问题。</p>
<p>如果你想改变某个dom元素的显示内容,比如一个view的显示文字:</p>
<p>以前是给view设id,然后js里通过选择器获取dom元素,进一步通过js进行赋值操作,修改dom元素的属性或值。</p>
<p>如下演示了一段代码,页面中有个显示的文字区和一个按钮,点击按钮后会修改文字区的值</p>
<p>现在的做法,是vue的绑定模式,给这个dom元素绑定一个js变量,在script中修改js变量的值,dom会自动变化,页面会自动更新渲染</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>{{textvalue}}<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 这里演示了组件值的绑定 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">:type</span><span style="color: rgba(0, 0, 255, 1)">="buttontype"</span><span style="color: rgba(255, 0, 0, 1)"> @click</span><span style="color: rgba(0, 0, 255, 1)">="changetextvalue()"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>修改为789<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 这里演示了属性和事件的绑定 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    export </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">default</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      data() {
</span><span style="color: rgba(0, 128, 128, 1)">11</span>             <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">return</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">                textvalue:</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">123</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">                buttontype:</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">primary</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">            };
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      },
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      onLoad() {
</span><span style="color: rgba(0, 128, 128, 1)">17</span>             <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">this</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">.textvalue</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">456</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">这里修改textvalue的值,其实123都来不及显示就变成了456</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      },
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      methods: {
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">            changetextvalue() {
</span><span style="color: rgba(0, 128, 128, 1)">21</span>               <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">this</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">.textvalue</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">789</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">//</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 128, 0, 1)">这里修改textvalue的值,页面自动刷新为789</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>如果你学过小程序的数据绑定,但不了解vue,要注意:</p>
<pre></pre>
<ul>
<li>
<p>小程序的数据绑定参考了vue,但自己修改了一些。在uni-app中只支持标准的vue,不支持小程序的数据绑定语法</p>
</li>
<li>
<p>小程序里的setData在uni-app里并不存在,因为vue是自动双向数据绑定的。直接通过赋值方式修改数据,如果数据绑定到界面上,界面会自动更新渲染</p>
</li>
<li>
<p>js api的变化</p>
</li>
</ul>
<p>因为uni-app的api是参考小程序的,所以和浏览器的js api有很多不同,如</p>
<ol>
<li>alert,confirm 改成&nbsp;uni.showmodel</li>
<li>ajax 改成&nbsp;uni.request</li>
<li>cookie、session 没有了,local.storage 改成&nbsp;uni.storage</li>
</ol>
<p>uni-app的js api还有很多,但基本就是小程序的api,把wx.xxx改为uni.xxx即可。详见</p>
<p>uni-app在不同的端,支持条件编译,无限制的使用各端独有的api,详见条件编译</p>
<h3>css的变化</h3>
<p>标准的css基本都是支持的。</p>
<p>选择器有2个变化:*选择器不支持;元素选择器里没有body,改为了page。微信小程序即是如此。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">page{
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> }</pre>
</div>
<p>单位方面,px无法动态适应不同宽度的屏幕,rem只能用于h5。如果想使用根据屏幕宽度自适应的单位,推荐使用rpx,全端支持。&nbsp;尺寸单位文档</p>
<p>uni-app推荐使用flex布局,并默认就是flex布局,这个布局思路和传统流式布局有点区别。但flex的有趣在于,不管是什么技术都支持这种排版,web、小程序/快应用、weex/rn、原生的iOS、Android开发,全都支持flex。它是通吃所有端的新一代布局方案。相关教程请自行百度学习。</p>
<p>uni-app的vue文件里支持所有web排版方式,不管是流式还是flex。但nvue里,只支持flex,因为它在app端是使用原生排版引擎渲染的。</p>
<p>注意背景图和字体文件尽量不要大于40k。会影响性能。如果非要大于40k,需放到服务器侧远程引用或base64后引入,不能放到本地作为独立文件引用。在小程序里,其实小于40k的文件在css里也无法引用,uni-app编译器在编译时自动做了处理,把小于40k的文件编译为base64方式了。</p>
<p>&nbsp;</p>
<h3>工程结构和页面管理</h3>
<p>uni-app的工程结构有单独的要求,详见</p>
<p>每个可显示的页面,都必须在&nbsp;pages.json&nbsp;中注册。如果你开发过小程序,那么pages.json类似app.json。如果你熟悉vue,这里没有vue的路由,都是在pages.json里管理。</p>
<p>原来工程的首页一般是index.html或default.html,是在web server里配的。而uni-app的首页,是在pages.json里配的,page节点下第一个页面就是首页。一般在/pages/xx的目录下。</p>
<p>app和小程序中,为了提升体验,页面提供了原生的导航栏和底部tabbar,注意这些配置是在pages.json中做,而不是在vue页面里创建,但点击事件的监听在显示的vue页面中做。</p>
<p>在vue中,以前的js<code>事件监听</code>概念改为了<code>生命周期</code>概念。详见uni-app生命周期</p>
<p>如果你熟悉小程序开发的话,对比变化如下:</p>
<ul>
<li>原来app.json被一拆为二。页面管理,被挪入了uni-app的pages.json;非页面管理,挪入了manifest.json</li>
<li>原来的app.js和app.wxss被合并到了app.vue中</li>
</ul>
<h3>结语</h3>
<p>最后,本文并非uni-app的完整教程,了解掌握uni-app,还需要认真过一遍uni-app的文档。</p>
<p>掌握好vue,也仍需要进一步去vue官网学习。或通过uni-app专业视频培训课程,连同vue带uni-app一起学会。</p>
<p>根据腾讯课堂和职友集的数据,会vue的人,比不会vue的普通前端薪资高27%哦。</p>
<p>如果你熟悉小程序,但不熟悉vue的话,这里还有一篇文章总结的很好:vue和微信小程序的区别、比较</p><br><br>
来源:https://www.cnblogs.com/KKSoft/p/11609988.html
頁: [1]
查看完整版本: uni-app学习资料整理-1.白话uni-app