美文网首页Vue让前端飞
vue组件模板的几种书写形式

vue组件模板的几种书写形式

作者: 卓三阳 | 来源:发表于2018-06-30 11:50 被阅读21次

1.第一种使用script标签

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <test-component></test-component>
        </div>
        <script type="text/x-template" id="testComponent"><!-- 注意 type 和id。 -->
            <div>{{test}} look test component!</div>
        </script>
    </body>
    <script>
        //全局注册组件
        Vue.component('test-component',{
            template: '#testComponent', 
            data(){
              return{
                test:"hello"
              }
            }
        })

        new Vue({
            el: '#app'
        })
    </script>
</html>

注意:使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,
浏览器在解析HTML文档时会忽略<script>标签内定义的内容。


2.第二种使用template标签

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <test-component></test-component>
        </div>

        <template id="testComponent">
            <div>look test component!</div>
        </template>
    </body>
    <script>

        Vue.component('test-component',{
            template: '#testComponent'
        })

        new Vue({
            el: '#app'
        })

    </script>
</html>

当然,如果template内容少的话,我们可以直接在组件中书写,而不需要用template标签。像下面这样:

 Vue.component('test-component',{
       template:`<h1>this is test,{{test}}</h1>`,
       data(){
          return{
             test:"hello test"
             }
            }
   })

3.第三种 单文件组件

这种方法常用在vue单页应用中
创建.vue后缀的文件,组件Hello.vue,放到components文件夹中

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: '欢迎!'
    }
  }
}
</script>

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <hello></hello>
  </div>
</template>

<script>

import Hello from './components/Hello'
export default {
  name: 'app',
  components: {
    Hello
  }
}
</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>

相关文章

  • vue组件模板的几种书写形式

    1.第一种使用script标签 注意:使用 标签时,type指定为text/x-template,意在告诉浏览器这...

  • vue开发组件设置*.vue文件模板

    title: vue组件开发模板date: 2016-11-21 开发模板 规范 本文介绍vue组件开发的模板,在...

  • Vue03

    Vue组件 一、组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是...

  • Vue (三)

    Vue组件 一、组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是...

  • 路由嵌套

    模板抽离 我们已经学习过了Vue模板的另外定义形式,使用。 然后js里定义路由组件的时候: // 1. 定义(路由...

  • 第十七讲、Vue3.x Teleport、使用Teleport自

    一、Vue3.x Teleport、Vue3.x中的组件模板属于该组件,有时候我们想把模板的内容移动到当前组件之外...

  • 从0搭建rollup+vue组件模板,轻松发布npm、githu

    最近更新模板 vue-cli3携手rollup、github-actions打造自动部署的vue组件模板(使用篇)...

  • 快速构建 vue 编码环境

    一、用 vue-cli 克隆一个官方的模板 安装vue-cli组件 创建一个官方的模板项目。 二、安装组件 三、编...

  • vue父组件调用子组件的方法

    vue组件与组件通信有如下几种情况: 平行组件父组件与子组件子组件与父组件 它们之间通信有几种方法有: props...

  • Vue3 现在有多少种写法?

    Vue3 中加入了响应式API,书写一个组件的方式又增加了几种。看看有什么不一样 vue2 和 vue3都适用 熟...

网友评论

    本文标题:vue组件模板的几种书写形式

    本文链接:https://www.haomeiwen.com/subject/yrgzyftx.html