[Vue] Vue-cli (andamiaje) realiza la invocación de componentes Vue de un solo archivo (gráficos y código)

1. Darse cuenta del efecto

 2. Pasos de implementación

1. Componente Vue de archivo único School.Vue (en componentes)

<template>
  <!-- 组件一 -->
  <div class="demo">
    <h2>---------【School.vue】---------</h2>
    <h2>学校名称:{
   
   { schoolName }}</h2>
    <h2>学校地址:{
   
   { address }}</h2>
    <button @click="showName">点我提示学校</button>
  </div>
</template>

<script>
// 把组件暴露出去,方便引入  Vue.extend可以省略
export default {
  name:"School",
  date() {
    return {
      schoolName: "清华大学",
      address: "北京",
    };
  },
  methods: {
    showName() {
      alert(this.schoolName);
    },
  },
};
</script>

<style>
.demo {
  background-color: antiquewhite;
  border: 1px red solid;
}
</style>

2. Componente Vue de un solo archivo Student.Vue (en componentes)

<template>
  <!-- 组件一 -->
  <div class="demo">
    <h2>---------【Student.vue】---------</h2>
    <h2>入取该校第1名学生:{
   
   { name1 }}</h2>
    <h2>入取该校第2名学生:{
   
   { name2 }}</h2>
    <h2>入取该校第3名学生:{
   
   { name3 }}</h2>
    <button @click="showName">点我提示第一名学生</button>
  </div>
</template>

<script>
// 把组件暴露出去,方便引入  Vue.extend可以省略
export default {
  name:"Student",
  date() {
    return {
      name1: "孔明",
      name2: "司马懿",
      name3:"曹操"
    };
  },
  methods: {
    showName() {
      alert(this.name1);
    },
  },
};
</script>

<style>
.demo {
  background-color: rgb(231, 231, 231);
  border: 1px rgb(172, 172, 172) solid;
}
</style>

3. Componente Vue de archivo único App.Vue

<template>
  <div>
    <img src="./assets/logo.png" style="width:130px;height:130px">
    <School></School>
    <Student></Student>


  </div>
</template>

<script>
// 引入组件
import School from './components/School.vue'
import Student from './components/Student.vue'
// 注册组件
export default {
  name: "App",
  components: {
    School,
    Student
  },
};
</script>

<style>
.app {
  background-color: rgb(0, 0, 0);
  border: 1px rgb(255, 255, 255) solid;
  height: 300px;
  width: 500px;
}
</style>

4. Nota: Los tres archivos anteriores se colocan en la posición de andamio

  5, principal.js

// 引入Vue
import Vue from 'vue'
// 引入app组件,它是所有组件的父组件
import App from './App.vue'
// 关闭vue生产提示
Vue.config.productionTip = false

// 创建vue实例对象 -- vm

new Vue({
  el:"#app",
  // 完成了这个功能:将APP组件放入窗口中
  render: h => h(App),
})

6.index.html (página de inicio en http://localhost:8080)

Aviso:

1. Llame al archivo en la carpeta pública, use: <%= BASE_URL %>

2. El contenido de <%= htmlWebpackPlugin.options.title %> se obtiene de [package.json]

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

Supongo que te gusta

Origin blog.csdn.net/dxnn520/article/details/123787206
Recomendado
Clasificación