花自飘零 發表於 2021-7-1 10:04:00

【干货】前端开发VUE实例

<p>  最近公司招聘前端开发----VUE方向。</p>
<p>  技术面试是必不可少的,发现大多数人应该没有掌握其开发技术,今天就大概总结一下</p>
<p>  一、准备工作</p>
<p>  需要用到VSCODE最新版,nodejs,vue2.0(现在vue3.0已经出来了,但是大多数公司还是用的vue2.0)</p>
<p>  具体安装配置就不说了,网上太多了,况且面试的电脑上一般都已经安装好了。</p>
<p>  二、创建工程</p>
<p>  vue2.0创建工程有三种方式:分别是用vue-cli脚手架、命令行、图形化ui</p>
<p>  这三种方式最快捷方便的就是第二种。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">vue create hello-world
cd hello-world
npm run serve// 运行http://localhost:8000
</pre>
</div>
<p>  这里说明一下三种方式的区别</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">1</span>、使用vue-<span style="color: rgba(0, 0, 0, 1)">cli脚手架创建新vuejs项目
用vue</span>-<span style="color: rgba(0, 0, 0, 1)">cli脚手架可以快速的构建出一个前端vue框架的项目结构

前提条件:已安装node可以正常使用npm命令,并全局安装vue</span>-<span style="color: rgba(0, 0, 0, 1)">cli工具。
nodejs可以到nodejs官网下载最新的版本,根据自己的操作系统选择合适的版本,然后安装,配置好环境变量即可。

全局安装vue</span>-<span style="color: rgba(0, 0, 0, 1)">cli脚手架
npm install vue</span>-cli -<span style="color: rgba(0, 0, 0, 1)">g
开始创建项目
使用vue初始化基于webpack的新项目
vue init webpack my</span>-<span style="color: rgba(0, 0, 0, 1)">project
项目创建过程中会提示是否安装eslint,可以选择不安装,否则项目编译过程中出现各种代码格式的问题;

项目创建完成后,安装基础模块
cd myproject
npm install
模块安装时间有可能会很长,依赖与网速;

安装完成之后可在开发模式下运行项目并预览项目效果
npm run dev;
如果项目可以正常启动,即可继续安装vue的辅助工具
npm install vue</span>-router --<span style="color: rgba(0, 0, 0, 1)">save (路由管理模块)
npm install vuex </span>--<span style="color: rgba(0, 0, 0, 1)">save (状态管理模块)
npm install vue</span>-resource --<span style="color: rgba(0, 0, 0, 1)">save (网路请求模块)
下面的两种方法是基于Vue Cli3的,
Vue CLI3 需要 Node.js </span><span style="color: rgba(128, 0, 128, 1)">8.9</span><span style="color: rgba(0, 0, 0, 1)"> 或更高版本

</span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">、使用vue create命令创建vue项目
vue create hello</span>-<span style="color: rgba(0, 0, 0, 1)">world<br><img src="https://img2020.cnblogs.com/blog/91995/202107/91995-20210701093038496-1712513214.png" alt="" loading="lazy"></span></pre>
<p>&nbsp;</p>
<pre></pre>
<p>&nbsp;安装向导,直接选择,非常方便</p>
<pre><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2020.cnblogs.com/blog/91995/202107/91995-20210701093313471-1855534148.png" alt="" loading="lazy"></span></pre>
<p>&nbsp;</p>
<pre></pre>
<p>&nbsp;</p>
<pre><span style="color: rgba(0, 0, 0, 1)">
cd hello</span>-<span style="color: rgba(0, 0, 0, 1)">world<br><img src="https://img2020.cnblogs.com/blog/91995/202107/91995-20210701093426144-917949215.png" alt="" loading="lazy"></span></pre>
<p>&nbsp;</p>
<pre><span style="color: rgba(0, 0, 0, 1)"></span></pre>
<p>&nbsp;</p>
<pre><span style="color: rgba(0, 0, 0, 1)">
npm run serve</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 运行</span>
<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">、使用vue cli3的ui命令基于图形用户界面创建vuejs项目
在命令行窗口中输入命令vue ui创建vuejs项目
vue ui </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自动运行图形页面 </span>
具体可以参考使用图形化界面创建vue项目这篇文章,傻瓜式操作,基于图片界面的,创建、安装依赖和插件很方便。</pre>
</div>
<p>  二、连接后台接口,即axios的使用</p>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_52eff47d-514c-4e12-8d15-752c747493b9" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_52eff47d-514c-4e12-8d15-752c747493b9" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_52eff47d-514c-4e12-8d15-752c747493b9" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;script&gt;
<span style="color: rgba(0, 128, 128, 1)"> 2</span> import axios from 'axios'
<span style="color: rgba(0, 128, 128, 1)"> 3</span> export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>   name: 'HelloWorld'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">props: {
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">    msg: String
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">},
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">data(){
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">          list:[],
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">},
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">created(){
</span><span style="color: rgba(0, 128, 128, 1)">14</span>   axios.post('http://192.168.0.3:9001/api/Camera/GetAll'<span style="color: rgba(0, 0, 0, 1)">,{})
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   .then((res)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">      console.log(res)
</span><span style="color: rgba(0, 128, 128, 1)">17</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.list=<span style="color: rgba(0, 0, 0, 1)">res.data.data;
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">    })
</span><span style="color: rgba(0, 128, 128, 1)">19</span>   
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">22</span> &lt;/script&gt;</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>  没有安装axios的先在命令中安装npm install axios,然后如上代码,在helloword.vue的SCRIPT中引入axios,然后在create中加入连接,并把得到的数据赋给变量list</p>
<p>  三、将得到的list变量,即JSON数据渲染到前端,为了说明问题,直接用table遍历。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;gutter:true;">&lt;template&gt;
&lt;div class="hello"&gt;
    &lt;h1&gt;{{ msg }}&lt;/h1&gt;
    &lt;table border="1"&gt;
      &lt;tbody&gt;
      &lt;th&gt;&lt;td&gt;area&lt;/td&gt;&lt;/th&gt;
      &lt;th&gt;&lt;td&gt;cameracode&lt;/td&gt;&lt;/th&gt;
      &lt;th&gt;&lt;td&gt;cameraname&lt;/td&gt;&lt;/th&gt;
      &lt;th&gt;&lt;td&gt;ip&lt;/td&gt;&lt;/th&gt;
      &lt;th&gt;&lt;td&gt;user&lt;/td&gt;&lt;/th&gt;
      &lt;th&gt;&lt;td&gt;pwd&lt;/td&gt;&lt;/th&gt;
      &lt;tr v-for="item in list" :key="item.Id"&gt;
      &lt;td&gt;{{item.Area}}&lt;/td&gt;
      &lt;td&gt;{{item.CarmeraCode}}&lt;/td&gt;
      &lt;td&gt;{{item.CarmeraName}}&lt;/td&gt;
      &lt;td&gt;{{item.Id}}&lt;/td&gt;
      &lt;td&gt;{{item.User}}&lt;/td&gt;
      &lt;td&gt;{{item.Pwd}}&lt;/td&gt;
      &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
&lt;/div&gt;

&lt;/template&gt;
</pre>
</div>
<p> &nbsp; &nbsp;四、如果深入一点可以使用常用的ui组件,如Element-ui</p>
<p>  直接到官网看文档,里面有详尽的代码可以直接复制。</p>
<p>  </p><br><br>
来源:https://www.cnblogs.com/weipt/p/14957513.html
頁: [1]
查看完整版本: 【干货】前端开发VUE实例