陈德坤写字的地方

每天记录一篇文章,给大脑充电


  • 首页

  • 标签

  • 分类

  • 归档

vue-cli3.0项目使用proxy跨域

发表于 2019-03-12

通过vue-cli3.x版本构建的项目使用proxy和以前的项目不同,而且3.x版本构建的时候可以选用typescript了。下面记录一下如何使用proxy跨域。

  • 首先在根目录创建vue.config.js文件,这个配置文件在运行项目的时候自动加载。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // vue.config.js
    module.exports = {
    devServer: {
    proxy: {
    '/api': {
    target: 'http://xxxx/device/', //对应自己的接口
    changeOrigin: true,
    ws: true,
    pathRewrite: {
    '^/api': ''
    }
    }
    }
    }
    }

这个文件还可以配置其它信息,比如修改端口号,默认是8080等等。

  • 最后在页面发送请求的时候:
    1
    2
    3
    axios.get('/api/getDataPoint').then(res => {
    console.log(res)
    })

虽然我们在控制台看到请求的地址还是原本的路径,但是已经代理请求到http://xxxx/device/getDataPoint,这样就可以解决跨域的问题了。

两种常见JS面向象写法

发表于 2019-02-22

基于构造函数

1
2
3
4
5
6
7
function Circle(r) { 
this.r = r;
}
Circle.PI = 3.14159;
Circle.prototype.area = function() {
return Circle.PI * this.r * this.r;
}

调用

1
2
var c = new Circle(1.0); 
console.log(c.area())//3.14159;

类Json写法

1
2
3
4
5
6
var Circle={ 
"PI":3.14159,
"area":function(r){
return this.PI * r * r;
}
};

调用

1
console.log(Circle.area(1.0));//3.14159

JS面向对象写法总结

发表于 2019-02-22

众所周知,JavaScript是一门面向对象的操作语言,而我们想要用JavaScript对象化写法的时候,不得不提出一个操作符,叫做new操作符,那么不用new操作符和用new操作符有什么区别呢?

用new和不用new的区别

首先,我们去看下new在JavaScript里面的用法,按照javascript语言精粹中所说,如果在一个函数前面带上new来调用该函数,那么将创建一个隐藏连接到该函数的prototype成员的新对象,同时this将被绑定到那个新对象上。这句话说得很抽象,我们根据代码来理解。

1
2
3
4
5
6
7
8
9
10
11
12
function foo(){
this.name = "John";
this.age = 25;
var born = 1994;
return this.age;
}
//没用new关键字
var foo2 = foo();
console.log(foo2);//23
//用new关键字
var foo3 = new foo();
console.log(foo3);//foo {name: "John", age: 23}

简单利用一般处理程序处理接口请求并返回数据

发表于 2019-02-21

在项目中新建一个一般处理程序,默认生成如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

创建一个客户端的http请求实例

获取当前Http请求的响应实例

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string htmlStr = string.Empty;
string requestUrl = "https://api.apiopen.top/recommendPoetry";
//创建一个客户端的Http请求实例
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "GET";

//获取当前Http请求的响应实例
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
{
htmlStr = reader.ReadToEnd();
}
//向浏览器传递数据
context.Response.Write(htmlStr);

Mac系统如何显示隐藏文件?

发表于 2019-02-19
  • 在终端(Terminal)输入如下命令,即可显示隐藏文件和文件夹

    1
    defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder
  • 如需再次隐藏原本隐藏的文件和文件夹,可以输入如下命令

    1
    defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder

Hello World

发表于 2019-02-19

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Dirk C

陈德坤写字的地方,是一个专注于前端开发学习笔记的博客,每天记录一篇文章,给大脑充电。作者陈德坤(Dirk C)。

6 日志
© 2019 Dirk C
由 Hexo 强力驱动
|
主题 — NexT.Muse v5.1.4