配置vue-resource root和authorization [英] Configuration for vue-resource root and authorization

查看:456
本文介绍了配置vue-resource root和authorization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看有关如何设置配置的vue-resource的以下文档: https://github.com/vuejs/vue-resource/blob/master/docs/config.md

I'm looking at the following documentation for vue-resource that describes how to set up configuration: https://github.com/vuejs/vue-resource/blob/master/docs/config.md

它说要使用通用授权值设置标题:

It says to set your headers with a common authorization value:

 Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

我猜这个基本YXBpOnBhc3N3b3Jk值只是一个例子,但这个值是什么,应该使用什么而不是它呢?

I am guessing this "Basic YXBpOnBhc3N3b3Jk" value is just an example, but what is this value for, and what should one use instead of it?

在同一页面上,我还看到了root的设置:

On the same page, I also see a setting for "root":

 Vue.http.options.root = '/root';

我理解这是指网络应用的网址。但是,为什么vue-resource需要我告诉它这个值呢?它用于什么?

I understand this to mean the web URL for the web app. However, why does vue-resource need me to tell it this value? What does it use it for?

推荐答案

通过向 Vue.http.headers.common添加标题您告诉vue-resource在每个请求中添加这些标头的对象。

By adding headers to the Vue.http.headers.common object you are telling vue-resource to add these headers in every request.

您还可以为每个请求添加标头:

You can also add headers to each request:

http({
 url: '/someUrl', 
 method: 'GET',
 headers: {
   Authorization: 'Basic YXBpOnBhc3N3b3Jk'
 }
})

关于示例中授权标头的值:它是带有用户名/密码的base64编码字符串。

About the value for the Authorization header in the example: It is a base64-encoded string with username/password.

window.atob('YXBpOnBhc3N3b3Jk') // => "api:password"

如果您在使用vue-resource时需要使用基本身份验证,则应提供您自己的用户名/密码。

If you need to use basic authentication when using vue-resource, you should provide your own username/password.

请注意,使用您的应用程序的每个人都可以查看用户名/密码。
有关详细信息,请参阅 https://en.wikipedia.org/wiki/Basic_access_authentication 关于基本身份验证。

Note that everyone who is using you application can view the username/password. See https://en.wikipedia.org/wiki/Basic_access_authentication for more information about basic authentiaction.

root-property可能是REST服务的主要端点。
而不是:

The root-property could be the main endpoint to your REST-service. Instead of:

http({
 url: 'http://api.domain.com/v1/someRoute'
});

您可以设置根端点:

Vue.http.options.root = 'http://api.domain.com/v1'

// will call http://api.domain.com/v1/someRoute
http({
 url: '/someRoute'
});

这篇关于配置vue-resource root和authorization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆