• Vue:第一个Vue程序和vue-router路由


     

     先下载Node.js,上面网址进行安装

     

    • 在cmd中运行node -v命令,查看是否能够输出版本号
    • 在cmd中运行npm -v命令,查看是否能够输出版本号

    在这里插入图片描述

    安装node.js淘宝镜像加速器(cnpm)

    1. # -g 就是全局安装
    2. npm install cnpm -g
    3. # 或使用如下语句解决npm速度慢的问题,但是每次install都需要
    4. npm install --registry=https://registry.npm.taobao.org
    • 安装vue-cli
    1. cnpm instal1 vue-cli-g
    2. #测试是否安装成功#查看可以基于哪些模板创建vue应用程序,通常我们选择webpack
    3. vue list

    在这里插入图片描述

    6.3 第一个vue-cli应用程序

     

     创建第一个Vue程序,需要先找好一个目录,进入那个目录下使用Windows命令:

    vue init webpack myvue

    全都选no

    之后项目创建成功,再进行配置项目环境 

     

    然后 ,进入创建好的项目,输入cd myvue:

    输入:npm install

    当出现问题时,它会给出提示我们按照提示来就行
    在这里插入图片描述

     

    在这里插入图片描述

    访问localhost:8080即可

     

     

     

    进入项目, 输入上述命令:npm install vue-router --save-dev

    如果运行错误,更换cnpm install vue-router --save-dev

    在启动Vue项目如果启动不了,运行错误,更换以下运行版本 npm i vue-router@3.1.3

    • 如果在一个模块化工程中使用它,必须要通过Vue.use()明确地安装路由功能
    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. Vue.use(VueRouter);

    测试路由:

     

     定义几个自己的组件 Content.vue 、Main.vue、Kuang.vue

    COntent.vue:

    1. <template>
    2. <h1>内容页</h1>
    3. </template>
    4. <script>
    5. export default {
    6. name: "Content.vue"
    7. }
    8. </script>
    9. <style scoped>
    10. </style>

    Main.vue:

    1. <template>
    2. <h1>首页</h1>
    3. </template>
    4. <script>
    5. export default {
    6. name: "Main"
    7. }
    8. </script>
    9. <style scoped>
    10. </style>

    Kuang.vue:

    1. <template>
    2. <h1>Kuangshen</h1>
    3. </template>
    4. <script>
    5. export default {
    6. name: "Kuang"
    7. }
    8. </script>
    9. <style scoped>
    10. </style>
    • 安装路由,在src目录下,新建一个文件夹:router,专门存放路由,配置路由index.js

     

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Content from '../components/Content'
    4. import Kuang from '../components/Kuang'
    5. import Main from '../components/Main'
    6. //安装路由
    7. Vue.use(VueRouter);
    8. //配置导出路由
    9. export default new VueRouter({
    10. routes:[
    11. {
    12. //路由路径
    13. path:'/content',
    14. name:'content',
    15. //跳转的组件
    16. component:Content
    17. },
    18. {
    19. //路由路径
    20. path:'/main',
    21. name:'content',
    22. //跳转的组件
    23. component:Main
    24. },
    25. {
    26. //路由路径
    27. path:'/kuang',
    28. name:'content',
    29. //跳转的组件
    30. component:Kuang
    31. }
    32. ]
    33. });
    • main.js中配置路由
    1. // The Vue build version to load with the `import` command
    2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
    3. import Vue from 'vue'
    4. import App from './App'
    5. import router from './router' //自动扫描里面的路由配置
    6. Vue.config.productionTip = false
    7. /* eslint-disable no-new */
    8. new Vue({
    9. el: '#app',
    10. //配置路由
    11. router,
    12. components: { App },
    13. template: '<App/>'
    14. })
    • App.vue中使用路由
    1. <template>
    2. <div id="app">
    3. <img src="./assets/logo.png">
    4. <h1>Vue-Router</h1>
    5. <router-link to="/main">首页</router-link>
    6. <router-link to="/content">内容页</router-link>
    7. <router-link to="/kuang">Kuang</router-link>
    8. <router-view></router-view>
    9. </div>
    10. </template>
    11. <script>
    12. export default {
    13. name: 'App',
    14. }
    15. </script>
    16. <style>
    17. #app {
    18. font-family: 'Avenir', Helvetica, Arial, sans-serif;
    19. -webkit-font-smoothing: antialiased;
    20. -moz-osx-font-smoothing: grayscale;
    21. text-align: center;
    22. color: #2c3e50;
    23. margin-top: 60px;
    24. }
    25. </style>

    index.html

     

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>myvue</title>
      </head>
      <body>
        <div id="app"></div>
        <!-- built files will be auto injected -->
      </body>
    </html>
    

    运行npm run dev,然后浏览器访问localhost:8080

     

    点击首页:

     

    点击内容页:

     

    点击kaung:

     

     

  • 相关阅读:
    超声波模块详细介绍(stm32循迹小车中超声波的介绍)
    java学习之SpringCloud Alibaba
    NAT地址转换,路由器作为出口设备,实现负载分担
    python基于PHP+MySQL的图书共享交流论坛
    数据挖掘与机器学习:Python机器学习软件包Scikit-Learn的学习与运用
    微信小程序 原生开发 实现弹窗遮罩层 并且在遮罩层内使用scroll-view实现滚动内容(包括图片)
    4.03 用户中心-订单管理功能开发
    面试:dex文件结构
    EagleSDR USB HAT FT600
    c++ opencv将彩色图像按连通域区分
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125487501