• 大师偷学:vue-cli构建SPA项目和SPA项目结构


    目录

    一,vue-cli构建SPA项目

    二,SPA项目结构

    三、SPA完成路由的开发

    四、嵌套路由的使用


    一,vue-cli构建SPA项目

            前提:

    想要实现vue-lic构建SPA项目,前提是搭建好nodeJS环境

    我们使用两个命令来测试一下:

       node -v
       npm -v

    如果出现版本号则表示nodeJS环境搭建好了

             1.1什么是Vue-cli?

      vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下:        

      vue init webpack xxx                    

       注1:xxx 为自己创建项目的名称

       注2:必须先安装vue,vue-cli,webpack,node等一些必要的环境

             1.2安装vue-cli

    命令:

    npm install -g vue-cli

    npm install webpack -g

    安装完成之后打开命令窗口并输入 vue -V(注意这里是大写的“V”),如果出现相应的版本号,则说明安装成功。然后可以看到我们的目录下出现了一些文件:

            1.3创建SPA项目 

    命令:

    vue init webpack spa0911 

    此命令用于创建SPA项目,它会在当前目录生成一个以“spa0911”命名的文件夹

     创建成功

            1.4 使用开发软件HBuilder x 右键导入项目

    二,SPA项目结构

    1)在项目的目录层次中打开cmd窗口来启动项目

    命令:
    npm run dev

    2)得到一个访问地址 

    3)访问地址:证明SPA项目创建成功!

     注2:vue-cli构建的项目,在控制台npm run dev启动后,默认的调试地址是8080端口的但是大部分时候,我们都要并行几个项目开发,很有可能已经占用了8080端口,所以就涉及到如何去更改调试地址的端口号了

            config --> index.js  

            dev: {

              // Paths

              assetsSubDirectory: 'static',

              assetsPublicPath: '/',

              proxyTable: {},

              host: 'localhost',

              port: 8083,       // 在这里修改端口号

              autoOpenBrowser: false,

              errorOverlay: true,

              notifyOnErrors: true,

            },

     项目结构解析

    三、SPA完成路由的开发

     spa的访问过程:

    1.访问index.html
    2.index.html通过main.js中的vue实例管理#app边界,
    同时指定App.vue模板
    3.App.vue中包含了logo图片以及锚点,而锚点与组件的对应关系存在router/index.js中,所有就指向了一个组件
    4.最终App.vue中就显示了logo图片以及hellowrld.vue的内容   

     当前后端分离时会启动两个一样的端口号 此时我们需要修改端口

     组件使用流程:

     APP.vue:

    1. <template>
    2. <div id="app">
    3. <router-link to="/Home">首页router-link>
    4. <router-link to="/About">关于本站router-link>
    5. <router-view/>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. name: 'App'
    11. }
    12. script>
    13. <style>
    14. #app {
    15. font-family: 'Avenir', Helvetica, Arial, sans-serif;
    16. -webkit-font-smoothing: antialiased;
    17. -moz-osx-font-smoothing: grayscale;
    18. text-align: center;
    19. color: #2c3e50;
    20. margin-top: 60px;
    21. }
    22. style>

    Home.vue

    1. <template>
    2. <div>
    3. 这是首页内容,最新内容
    4. div>
    5. template>
    6. <script>
    7. export default{
    8. name:"Home",
    9. data(){
    10. return {
    11. msg:'Welcome to Your Vue.js App'
    12. };
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

     About.vue:

    1. <template>
    2. <div>
    3. 吾乃钮祜禄甄强!!!
    4. div>
    5. template>
    6. <script>
    7. export default{
    8. name:"About",
    9. data(){
    10. return {
    11. msg:'Welcome to Your Vue.js App'
    12. };
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

    index.js:

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import Home from '@/components/Home'
    5. import About from '@/components/About'
    6. Vue.use(Router)
    7. export default new Router({
    8. routes: [
    9. {
    10. path: '/Home',
    11. name: 'Home',
    12. component: Home
    13. },
    14. {
    15. path: '/About',
    16. name: 'About',
    17. component: About
    18. }
    19. ]
    20. })

     

    四、嵌套路由的使用

    其实嵌套路由就是在index.js中加入一个children的属性以及配置相关的关系
    About.vue:

    1. <template>
    2. <div>
    3. <router-link to="/AboutMe">关于站长router-link>
    4. <router-link to="/AboutWebSeit">关于本站router-link>
    5. <router-view>router-view>
    6. div>
    7. template>
    8. <script>
    9. export default{
    10. name:"About",
    11. data(){
    12. return {
    13. msg:'Welcome to Your Vue.js App'
    14. };
    15. }
    16. }
    17. script>
    18. <style>
    19. style>

    AboutMe.vue:

    1. <template>
    2. <div>
    3. 吾乃钮祜禄甄强!!!
    4. div>
    5. template>
    6. <script>
    7. export default{
    8. name:"AboutMe",
    9. data(){
    10. return {
    11. msg:'Welcome to Your Vue.js App'
    12. };
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

    AboutWebSeit.vue:

    1. <template>
    2. <div>
    3. 再见了!!!朕的江山!!!
    4. div>
    5. template>
    6. <script>
    7. export default{
    8. name:"AboutWebSeit",
    9. data(){
    10. return {
    11. msg:'Welcome to Your Vue.js App'
    12. };
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

    index.js:

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import Home from '@/components/Home'
    5. import About from '@/components/About'
    6. import AboutMe from '@/components/AboutMe'
    7. import AboutSeit from '@/components/AboutWebSeit'
    8. Vue.use(Router)
    9. export default new Router({
    10. routes: [
    11. {
    12. path: '/Home',
    13. name: 'Home',
    14. component: Home
    15. },
    16. {
    17. path: '/About',
    18. name: 'About',
    19. component: About,
    20. children:[
    21. {
    22. path: '/AboutMe',
    23. name: 'AboutMe',
    24. component: AboutMe
    25. },
    26. {
    27. path: '/AboutWebSeit',
    28. name: 'AboutWebSeit',
    29. component: AboutWebSeit
    30. }
    31. ]
    32. }
    33. ]
    34. })
  • 相关阅读:
    2.4 自定义msg Python
    Linux系统下安装go
    基金合作保密协议
    go test 命令详解
    如何通过对话式机器人流程自动化 (CRPA),改善客户服务?
    人大女王金融硕士项目——比努力更重要的,是要学会做对的选择
    PAT 1141 PAT Ranking of Institutions
    JSON.stringify()和JSON.parse()的使用
    Spring boot 实践(16)Nacos server 2.2.3 下载安装
    String.format()的详细用法
  • 原文地址:https://blog.csdn.net/weixin_66110079/article/details/126802651