小编zva*_*dym的帖子

使用axios强制下载GET请求

我正在使用vuejs 2 + axios.我需要发送一个get请求,将一些params传递给服务器,并获得一个PDF作为响应.服务器使用Laravel.

所以

axios.get(`order-results/${id}/export-pdf`, { params: { ... }})
Run Code Online (Sandbox Code Playgroud)

成功请求,但它不会启动强制下载,即使服务器返回正确的标头.

我认为这是一种典型情况,例如,您需要形成PDF报告并将一些过滤器传递给服务器.那怎么可能实现呢?

更新

所以实际上我找到了解决方案.然而同样的方法不适用于axios,不知道为什么,这就是我使用原始XHR对象的原因.所以解决方案是创建一个blob对象和用户createUrlObject函数.示例示例:

let xhr = new XMLHttpRequest()
xhr.open('POST', Vue.config.baseUrl + `order-results/${id}/export-pdf`, true)
xhr.setRequestHeader("Authorization", 'Bearer ' + this.token())
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xhr.responseType = 'arraybuffer'

xhr.onload = function(e) {
  if (this.status === 200) {
    let blob = new Blob([this.response], { type:"application/pdf" })
    let link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    link.download = 'Results.pdf'
    link.click()
  }
}
Run Code Online (Sandbox Code Playgroud)

重要提示:您应该将数组缓冲区作为响应类型

但是,在axios中编写的相同代码返回的PDF为空:

axios.post(`order-results/${id}/export-pdf`, {
  data,
  responseType: 'arraybuffer'
}).then((response) => {
  console.log(response)

  let blob …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js axios vuejs2

31
推荐指数
4
解决办法
5万
查看次数

ES6代码样式最佳实践

最近我开始学习ReactJS,因此 - ES6.我对ES5非常熟悉,但有些事情对我来说并不是那么清楚.

示例1:方法语法

以下两种方法有什么区别?

export class InvoiceForm extends React.Component {
    methodName1() {
    }

    methodName2 = () => {

    };
}
Run Code Online (Sandbox Code Playgroud)

示例2:外部的类属性

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.propTypes = {
  name: PropTypes.string
};
Run Code Online (Sandbox Code Playgroud)

propTypes在课外.但为什么?我来自python和我一样,以下更正确

class Greeting extends React.Component {
  static propTypes = {
    name: PropTypes.string
  }
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-5 ecmascript-6

6
推荐指数
1
解决办法
1145
查看次数

如何隔离Vuetify整体风格

我已经开始在一个旧的现有项目中使用Vue.js和Vuetify。因此,我没有重写所有前端,而是导入了Vue并替换了某些部分。

然后,我注意到了非常意外的行为-Vuetify具有通用类的全局样式,.title并且影响整个页面,而不仅是Vue部分页面。

因此,问题是,如何隔离Vue组件内的vuetify样式?


UPD:根据建议@DigitalDrifter,我尝试使用stylus块级导入。所以我删除了

import 'vuetify/dist/vuetify.min.css' 
Run Code Online (Sandbox Code Playgroud)

main.js并创建一个.styl具有以下内容的新文件(而不是导入的CSS):

.vuetify-styles
    @import '~vuetify/src/stylus/main'
Run Code Online (Sandbox Code Playgroud)

然后将此类添加到根组件: <App class="vuetify-styles">

UPD2:之后,您将获得与stylus编译相关的错误。有关它的更多信息-> https://github.com/vuetifyjs/vuetify/issues/4864

css vue.js vuetify.js

6
推荐指数
1
解决办法
1109
查看次数

如何将 ReactJS 添加到现有的 node.js 后端应用程序

我有一个非常简单的 node.js API

结构:

| project-name
|  public
|     index.html
|     ... some static js/css
|  app.js
|  package.json
Run Code Online (Sandbox Code Playgroud)

应用程序.js

var express = require('express'),
  bodyParser = require('body-parser'),
  http = require('http');

var app = module.exports = express();
app.set('port', process.env.PORT || 8000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static(path.join(__dirname, 'public')));

app.route('/api/projects').get(...).post(...)
app.route('/api/sales').get(...).post(...)

app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// Starting express server
http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});
Run Code Online (Sandbox Code Playgroud)

运行服务器 - node app.js

注意:这是一个学习项目,不用于生产。我只想弄清楚这一切是如何协同工作的。

现在我想添加 …

javascript node.js express reactjs

5
推荐指数
1
解决办法
4657
查看次数

递归功率函数:方法

我现在正在编程一段时间(初学者),递归函数对我来说是一个有点抽象的概念.我不会说我卡住了,程序运行正常,我只是想知道函数本身是否可以在代码中没有pow函数的情况下编写(但仍然正是在做问题所表明的)

问题:http: //prntscr.com/30hxg9

我的解决方案

#include<stdio.h>
#include<math.h>

int power(int, int);

int main(void)
{
    int x, n;
    printf("Enter a number and power you wish to raise it to: ");
    scanf_s("%d %d", &x, &n);
    printf("Result: %d\n", power(n, x));
    return 0;
}

int power(int x, int n)
{
    if (n == 0) return 1;
    if (n % 2 == 0) return pow(power(x, n / 2), 2);
    else return x * power(x, n - 1);
}
Run Code Online (Sandbox Code Playgroud)

我试过这样做:power(power(x,n - 1),2); 但是执行失败了,我仍在回溯原因.

c recursion pow

2
推荐指数
2
解决办法
5万
查看次数