>  기사  >  웹 프론트엔드  >  Vue 컴포넌트를 작성하는 방법은 무엇입니까?

Vue 컴포넌트를 작성하는 방법은 무엇입니까?

php中世界最好的语言
php中世界最好的语言원래의
2018-04-14 11:49:201778검색

이번에는 Vue 컴포넌트 작성 방법과 Vue 컴포넌트 사용 시 주의사항에 대해 알려드리겠습니다. 다음은 실제 사례입니다.

첫 번째 방법스크립트 태그 사용

<!DOCTYPE html>
<html>
  <body>
    <p id="app">
      <my-component></my-component>
    </p>
    <-- 注意:使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,浏览器在解析HTML文档时会忽略<script>标签内定义的内容。-->
    <script type="text/x-template" id="myComponent">//注意 type 和id。
      <p>This is a component!</p>
    </script>
  </body>
  <script src="js/vue.js"></script>
  <script>
    //全局注册组件
    Vue.component('my-component',{
      template: '#myComponent'
    })
    new Vue({
      el: '#app'
    })
  </script>
</html>

두 번째 방법템플릿 태그 사용하기

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
  </head>
  <body>
    <p id="app">
      <my-component></my-component>
    </p>
    <template id="myComponent">
      <p>This is a component!</p>
    </template>
  </body>
  <script src="js/vue.js"></script>
  <script>
    Vue.component('my-component',{
      template: '#myComponent'
    })
    new Vue({
      el: '#app'
    })
  </script>
</html>

세 번째 유형 단일 파일 구성 요소

이 방법은 Vue 단일 페이지 애플리케이션에서 일반적으로 사용됩니다. 자세한 내용은 공식 홈페이지를 참고해주세요: https://cn.vuejs.org/v2/guide/single-file-comComponents.html

접미사가 .vue인 파일, Hello.vue 컴포넌트를 생성하고 컴포넌트 폴더

<template>
 <p class="hello">
  <h1>{{ msg }}</h1>
 </p>
</template>
<script>
export default {
 name: 'hello',
 data () {
  return {
   msg: '欢迎!'
  }
 }
}
</script>

에 넣습니다. app.vue

<!-- 展示模板 -->
<template>
 <p id="app">
  <img src="./assets/logo.png">
  <hello></hello>
 </p>
</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>

이 기사의 사례를 읽으신 후 방법을 마스터하셨다고 믿습니다. 더 흥미로운 정보를 보려면 PHP 중국어 웹사이트의 다른 관련 기사를 주목하세요!

추천 도서:

js에서 비동기 메서드를 구현하기 위해 초보자가 꼭 읽어야 할 책

JS를 통해 JSON 데이터를 얻고 로드하는 단계에 대한 자세한 설명

위 내용은 Vue 컴포넌트를 작성하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.