1 模块化

//es6 模块化的方案

1-1 导出

  1. //node.js
  2. module.exports = {};
  3. //es6
  4. export default {
  5. }


1-4 导入

  1. //node.js
  2. const app = require("./App.vue");
  3. //es6
  4. import app from './App.vue';


2 项目说明

//main.js
1-2 安装依赖 - 图1

3 组件语法

  1. # 1.在组件的html中,根元素的div只能有一个
  2. # 2.js中,data是函数
  3. # 3.for循环必须加key
  4. <template>
  5. <div>
  6. {{ msg }}
  7. <div v-for="item of arr" :key="item.id">{{item.name}}</div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: "App",
  13. data() {
  14. return {
  15. msg: "data",
  16. arr:[
  17. {name:"html",id:1001},
  18. {name:"css",id:1002}
  19. ]
  20. };
  21. },
  22. mounted() {
  23. http();
  24. },
  25. };
  26. </script>

4 安装其他的模块

1-1 安装

yarn add axios
cnpm i axios -S

1-2 导入模块发送http请求

<template>
  <div>
     hello world
  </div>
</template>
<script>
import axios from 'axios';
export default {
  name: "App",
  mounted(){
    let url  = "http://192.168.4.20:8000/top250";
    axios.get(url).then(res=>{
      console.log(res)
    })
  }
};
</script>

5 安装的模块挂载到vue的原型上

//main.js
import axios from 'axios';
Vue.prototype.$http = axios;
//App.vue
export default {
  mounted(){
    this.$http.get(url).then(res=>{
      console.log(res)
    })
  }
}