2017-06-18 87 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' 
相关问题