vue入门学习2-在模板例程里添加一个组件

vue入门学习2-在模板例程里添加一个组件

vue入门学习2-在模板例程里添加一个组件

1.初始基本项目文件结构说明

在上一篇使用vue init webpack {项目名} 利用vue脚手架工具、webpack模板构建了基本项目后,项目文件结构大体如下:
在这里插入图片描述
下面就几个主要文件作一个说明:
main.js–程序入口文件

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

在这个程序文件里面:
(1)新建了主vue实例
new Vue()
(2)挂载了主组件:App
el:’#app’
components:{App}

2.添加新组件

(1)编写新增的组件文件

在 src/components目录下编写 EmployeeTable.vue 组件文件。我直接从参考网站拷贝 _:
src/components/EmployeeTable.vue:

<template>
  <div id="employee-table">
    <table>
      <thead>
        <tr>
          <th>Employee name</th>
          <th>Employee email</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Richard Hendricks</td>
          <td>richard@piedpiper.com</td>
        </tr>
        <tr>
          <td>Bertram Gilfoyle</td>
          <td>gilfoyle@piedpiper.com</td>
        </tr>
        <tr>
          <td>Dinesh Chugtai</td>
          <td>dinesh@piedpiper.com</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
  export default {
    name: 'employee-table',
  }
</script>

<style scoped></style>

(2)注册新增组件

在要调用新增组件的主组件(以下简称主组件)文件的脚本部分注册新增组件
在本例中主组件就是App组件,在App.vue文件里面的部分增加注册"employee-table"组件的内容:
在这里插入图片描述

(3)使用新增组件

在主组件文件的模板部分使用新增组件
在这里插入图片描述

3.运行测试

(1) 关闭eslint

先关闭eslint,否则因为编译执行严格的检查,会报各种不必要的错误。
编辑config/index.js把 useEslint: true 改为: useEslint: false

(2) 运行

npm run dev

在这里插入图片描述
上一篇 Vue入门学习1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值