Vue.js组件之间的调用

本文详细介绍了Vue.js项目的启动过程,从index.html到main.js的执行,再到各个组件如top.vue、footerbottom.vue及router-view的加载方式。通过具体的代码示例,展示了Vue项目中组件间的依赖关系及路由配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.html:

<div id="app"></div>

运行完index.html之后自动寻找运行main.js文件

main.js:

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

然后进入app.vue

app.vue:

<template>
  <div id="app">
    <top></top>
    <router-view></router-view>
    <footerbottom>
    </footerbottom>
  </div>
</template>

<script>
import Top from './top'
import Footerbottom from './footerbottom'
export default {
  name: 'app',
  components: {
    Top,
    Footerbottom
  }
}
</script>

根据标签的结构走,<top></top>放top.vue的内容

top.vue:

<template>

<h2>

top

</h2>

</template>
<script>
export default({
  name: 'top'
})
</script>

<footerbottom></footerbottom>放footerbottom.vue的内容

footerbottom.vue:

<template>

<h2>

bottom

</h2>

</template>
<script>
export default({
  name: 'footerbottom'
})
</script>

<router-view></router-view>默认指向router文件夹下的index.js文件,

index.js:

import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/hello'

Vue.use(Router)

export default new Router({
  routes: [
    { path: '/', component: Hello }
  ]
})

路由指向/components/hello.vue文件

hello.vue:

<template>

<p>

Hello world!

</p>

</template>
<script>
export default {
  name: 'hello'
}
</script>

最后运行出来的效果如图:



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值