2016-11-23 70 views
1

在我VUE組件的js代碼,我收到此錯誤:無效的或意外的標記JavaScript文件

Uncaught SyntaxError: Invalid or unexpected token

這是我的代碼:

Vue.component('pickpoint-info', { 

    template : '<table>\ // in this line I get the error 
        <tbody>\ 
         <tr v-for="item in items" v-on:click="selectPickPoint(this)">\ 
          <td width="50" align="center"><input name="pickpointid" type="radio" value="{{ item.customer_id }}"></td>\ 
          <td width="300">\ 
           <b>{{ item.name }}</b> <i style="font-size:9px;">#{{ item.customer_id }}</i>\ 
           <br>\ 
           {{ item.address }}\ 
           <br>\ 
           {{ item.postal_code }}\ 
           <a href="{{ item.map_link }}" target="_blank" style="font-size:10px;">(ver en el mapa</a>)\ 
          </td>\ 
         </tr>\ 
        </tbody>\ 
       </table>\ 
    ', 

有什麼不對的碼?

回答

0

儘量避免使用字符串作爲模板,而是使用HTML 5模板標籤,並引用它:

<template id="my-template"> 
    <table> in this line I get the error 
    <tbody> 
     <tr v-for="item in items" v-on:click="selectPickPoint(this)"> 
     <td width="50" align="center"> 
      <input name="pickpointid" type="radio" value="{{ item.customer_id }}"> 
     </td> 
     <td width="300"> 
      <b>{{ item.name }}</b> <i style="font-size:9px;">#{{ item.customer_id }}</i> 
      <br> {{ item.address }} 
      <br> {{ item.postal_code }} 
      <a href="{{ item.map_link }}" target="_blank" style="font-size:10px;">(ver en el mapa</a>) 
     </td> 
     </tr> 
    </tbody> 
    </table> 
</template> 

然後你就可以id引用它:

Vue.component('pickpoint-info', { 
    template: "#my-template" 
}); 

如果你想使用一個字符串,然後檢查您的行的結束,並確保反斜槓後沒有空格,我認爲你的錯誤在於這兩行:

{{ item.address }}\ 
    <br>\ 

兩者都有whitespace最後需要刪除。

+0

或者,如果他想要去的字符串,有作爲ES2015.But的一部分,我同意你的專用模板字符串 - 關於使用HTML5'template'標籤。 –

+0

是的,雖然它們需要被編譯,甚至默認情況下'babel'不包含'template literals'轉換,所以如果你這樣做,你可能會使用一個單一的頁面組件,它更清潔。 –

+0

嗯,但這是一個組件,我不在這個視圖中使用html。在任何html文件中,我都想使用標籤並對組件進行渲染。 –

3

你必須寫反引號`,而不是簡單的報價「

+0

這是一個被接受的答案 – Khaleel

相關問題