简体   繁体   English

我可以将我现有的 vue 组件用作单个文件组件吗

[英]Can I use my existing vue component as a single file component

So I had a vue app that I just wrote into a .html file being served by django.所以我有一个 vue 应用程序,我刚刚将它写入 django 提供的 .html 文件中。 I now am trying to move over to a dedicated vue.js project with the CLI and so I'm wanting to break apart all the components I had in that single file and move them into their own vue files.我现在正在尝试使用 CLI 转移到一个专用的 vue.js 项目,因此我想分解我在该单个文件中拥有的所有组件并将它们移动到它们自己的 vue 文件中。

My question is can I just create a file called Overview.vue and copy and paste my component into there as is?我的问题是我可以只创建一个名为 Overview.vue 的文件并将我的组件复制并粘贴到那里吗? - or do I need to make some modifications? - 还是我需要做一些修改?

Most examples I see of single file components have dedicated blocks for <style> <template> <script> .我看到的大多数单文件组件示例都有<style> <template> <script>的专用块。 In my component I pasted below, the template is inside the component.在我粘贴在下面的组件中,模板位于组件内部。 Do I need to change this?我需要改变这个吗?

Vue.component('overview', {
  delimiters: [ '[[', ']]' ],
  props: ['jobs', 'links'],
  template: `
  <div overview>
  <h3>Overview</h3>
  <table :style="overviewStyle">
  <tr>
  <td>Start Time</td>
  <td>[[ jobs[0].time_start ]]</td>
  </tr>
  </table>
  </div>
  `,
  computed: {
    overviewStyle() {
      return {
        'padding': '8px',
        'width': '100%',
        'display': 'table',
      };
    },
  methods: {
    getStyle (name) {
      switch (name) {
        case 'SUCCESS':
        return {
          'background-color': '#dff0d8',
          'padding': '10px',
          'line-height': '1.42857143',
          'border': '1px solid #C0C0C0',
        }
        case 'IN_PROGRESS':
        return {
          'background-color': '#f4dc42',
          'padding': '10px',
        }
        case 'FAILURE':
        return {
          'background-color': '#f45942',
          'padding': '10px',
          'line-height': '1.42857143',
          'border': '1px solid #C0C0C0',
        }
      };
    },
  },
});

Yes, you will need to follow the structure for Vue Single File Components .是的,您需要遵循Vue Single File Components的结构。

It accepts 3 blocks, <template> , <script> and <style> .它接受 3 个块, <template><script><style>

<template>
  <!-- your template here -->
</template>

<script>
  // javascript here
  export default {
    data() {
      return { ... }
    },
    methods: { ... }
    // etc
  }
</script>

<style>
  /* any css here */
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM