vue 入门开发

1.安装
npm install vue-cli -g

安装存在的问题(全局变量)

提示vue,command not found,或者类似的都是因为环境边梁没有配置

1. npm没有加入到环境变量中
2. 想要用到的包没有加入到环境变量中
3. C:Program Files
odejs;C:Program Files (x86)Bracketscommand;C:Program Files (x86)Yarnin;C:UsersAdministratorAppDataRoaming
pm;
4.  搜索vue.cmd 将路径加入Path用户环境变量中(不建议加入系统环境变量)。如我的路径是C:UsersAdministratorAppDataRoaming
pm;

一个vue模板

Test.vue

<template>
    <div class="test">
        <h1>Test</h1>
    </div>
</template>
<script>
export default {
  name: "Test",
  data() {
    return {
      msg: "Welcome to Your Vue.js App"
    };
  }
};
</script>
<style scoped>
h1,
h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
    <test></test>
  </div>
</template>

<script>
import Test from './components/Test'
export default {
  name: "App",
  components:{
    Test
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

父子组件通信

父组件

:msg 传值给子组件
<template>
  <div class="hello">
    <h2>Essential Links</h2>
    <test :msg="msg" :getInfo="getInfo"/>
  </div>
</template>

<script>
import Test from "./Test";
export default {
  name: "HelloWorld",
  components: {
    Test
  },
  data() {
    return {
      msg: "父子组件通信",
      getInfo(){
        console.log('点击');
      },
    };
  },
};
</script>

子组件

props:["msg"]进行接收,"msg"读值,"getInfo()"触发方法
<template>
    <div class="test">
        <h1>{{msg}}</h1>
        <h1>{{msgChildren}}</h1>
        <h1 @click="getInfo()">子组件点击</h1>
        <router-view/>
    </div>
</template>
<script>
export default {
  name: "test",
  data() {
    return {
      msgChildren: "Welcome to Your Vue.js App go"
    };
  },
  props: ["msg","getInfo"]
};
</script>

路由 router-view

{
      path: '/test',
      name: 'Test',
      component: Test,
      children: [{
        path: 'first',
        name: 'firstInfo',
        component: First
      }]
    }
    

test.vue

<template>
    <div class="test">
        <h1>{{msg}}</h1>
        <router-view/>
    </div>
</template>

访问/test 会展示test内容,访问/test/first 会将子组件加载到router-view到页面中

  • 路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容

  • 点击之后,怎么做到正确的对应,比如,我点击home 按钮,页面中怎么就正好能显示home的内容。这就要在js 文件中配置路由。

路由中有三个基本的概念 route, routes, router。

1, route,它是一条路由,由这个英文单词也可以看出来,它是单数, Home按钮 => home内容, 这是一条route, about按钮 => about 内容, 这是另一条路由。

2, routes 是一组路由,把上面的每一条路由组合起来,形成一个数组。[{home 按钮 =>home内容 }, { about按钮 => about 内容}]

3, router 是一个机制,相当于一个管理者,它来管理路由。因为routes 只是定义了一组路由,它放在哪里是静止的,当真正来了请求,怎么办? 就是当用户点击home 按钮的时候,怎么办?这时router 就起作用了,它到routes 中去查找,去找到对应的 home 内容,所以页面中就显示了 home 内容。

4,客户端中的路由,实际上就是dom 元素的显示和隐藏。当页面中显示home 内容的时候,about 中的内容全部隐藏,反之也是一样。客户端路由有两种实现方式:基于hash 和基于html5 history api.

vue-router中的路由也是基于上面的内容来实现的

在vue中实现路由还是相对简单的。因为我们页面中所有内容都是组件化的,我们只要把路径和组件对应起来就可以了,然后在页面中把组件渲染出来。

原文地址:https://www.cnblogs.com/Hsong/p/9581045.html