2017-06-18 91 views
4

我開始使用Typescript並嘗試將其應用到我的項目中。但是,我無法獲得像vue-resource這樣的Vue.js插件來處理它。在Typescript中使用Vue.js時如何使用像vue-resource這樣的插件?

當我使用

this.$http.post() 

我得到的錯誤:

error TS2339: Property '$http' does not exist on type 'typeof Vue'.

這是有道理的,因爲我在一類環境。但我該怎麼做?這是我的全部成分:

<template> 
<div> 
    <h1>Sign up</h1> 

    <form> 
    <div class="form-group"> 
     <label for="name">Name</label> 
     <input v-model="name" type="text" class="form-control" name="name" placeholder="Name"> 
     <small class="form-text text-muted">Please provide a name.</small> 
    </div> 
    <div class="form-group"> 
     <label for="name">Password</label> 
     <input v-model="password" type="password" class="form-control" name="password" placeholder="Password"> 
     <small class="form-text text-muted">Please provide a password.</small> 
    </div> 
    <input type="submit" class="btn btn-primary" value="Submit" @click.prevent="save"> 
    </form> 
</div> 
</template> 

<script lang="ts"> 
import Component from 'vue-class-component' 

@Component 
export default class SignUp extends Vue { 
    name: string = '' 
    password: string = '' 

    save(): void { 
    this.$http.post('/api/sign-up', { 
     name: this.name, 
     password: this.password 
     }) 
     .then((response: any) => { 
     console.log(response) 
     }) 
    } 
} 
</script> 

我登記我的main.ts VUE資源是這樣的:

import Vue from "vue" 
import router from "./router" 
import App from "./app" 

const VueResource = require('vue-resource') 

Vue.use(VueResource) 

new Vue({ 
    el: "#app", 
    router, 
    template: "<App/>", 
    components: { App }, 
}); 
+1

如果使用'import'語句而不是'require'導入VueResource,會發生什麼?我不使用打字稿,但'$ http'未定義,在js中使用'require'時; '進口'工作正常。 –

+0

是的!那樣做了!非常感謝。由於一些較舊的錯誤,我不得不'要求',但現在它似乎工作。您可以將您的評論轉換爲答案。祝你今天愉快。 – Julian

回答

2

使用import而不是require爲VueResource了。

import VueResource from 'vue-resource' 
相關問題