在我正在开发的应用程序中,我必须存储一些特定用户保持登录到应用程序的时间,不幸的是,在Web应用程序中,用户可以通过多种方式注销.
第一个很容易,因为应用程序可以控制注销过程.但在其他方面,它变得棘手.
你会怎么做才能解决这个问题?
我正在使用Laravel后端API开发Vue应用程序。单击链接后,我想调用服务器以下载特定文件(大多数情况下为PDF文件)。当我get向axios我发送请求时,会在响应的正文中得到一个PDF作为回报。我想直接下载该文件。
让您更好地了解响应的外观:
(注意:我知道真实的文本响应比图像要好,但是由于实际PDF内容的长度,我看不到任何返回方法。)
是否可以使用JavaScript或其他方式下载该文件?它必须是直接下载的特定内容,而无需再次单击该按钮。
码
// This method gets called when clicking on a link
downloadFile(id) {
const specificationId = this.$route.params.specificationId;
axios
.get(`${this.$API_URL}/api/v1/suppliersmanagement/product-specifications/${specificationId}/fileupload/${id}/download`, {
headers: this.headers,
})
.then(response => {
console.log(response);
// Direct download the file here..
})
.catch(error => console.log(error));
},
Run Code Online (Sandbox Code Playgroud) 我有一个d3(版本3.5)多线图,我试图在y轴上有5个刻度.
这是y轴生成的代码,带有ticks数:
问题是我输入的任何滴答值,在3到6之间,给出了相同的结果(比我想要的滴答更少):
当我输入7个滴答值时,它会得到以下结果(这比我想要的多得多):
如何强制图表在y轴上具有确切的刻度数?
谢谢.
根据 MDN set Method for Map,在 javascript 中向地图添加键/值对的唯一方法是set方法。我想知道当我们添加带有方括号的键/值对时,映射的行为是什么,如下所示;
const testMap = new Map();
testMap.set( 1,"firstValue" );
testMap[2] = "secondValue";
console.log( testMap );
console.log( testMap[ 2 ] );
console.log( testMap[ '2' ] );
Run Code Online (Sandbox Code Playgroud)
看来我们可以同时拥有地图和物体!有人可以向我解释一下吗?我知道 Map 是一种对象,但这种行为可能会导致很多错误。有什么办法可以防止这种情况发生吗?顺便说一句,如果您添加带有方括号的键/值对,则无法使用get方法检索它。
我有一个带动态键的对象数组
response = [{"1": 1}, {"2": 1}, {"3": 1}, {"4": 0}, {"5": 0}];
Run Code Online (Sandbox Code Playgroud)
我想将这个对象数组展平为一个数组
Output = [1, 1, 1, 0, 0]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
const test2 = this.response.map(obj => {
Object.keys(obj).map(function(key){
return obj[key]
})
});
const test = this.response.reduce(function(prev, curr){
console.log(curr)
return (curr) ? prev.concat(curr): prev;
},[]);
Run Code Online (Sandbox Code Playgroud) 目前正在使用Angular,socket.io和express创建应用程序.但是,我遇到了一个异步问题,我很难找到解决方案.这是代码:
export class WebsocketService {
this.socket;
public connect() {
const token = sessionStorage.getItem('token');
this.socket = io('http://localhost:3000', { query: { token: token } });
}
public getSocket () {
// this function should only return this.socket when it is available
return this.socket;
}
Run Code Online (Sandbox Code Playgroud)
这个想法是,首先在应用程序的某个地方与websocket连接一次,因此io函数被调用一次:
this.socket = io('http://localhost:3000', { query: { token: token } });
Run Code Online (Sandbox Code Playgroud)
然后在应用程序的其余部分中,this.socket应该传递属性.但是,this.socket应始终返回对象,如果不存在则应等待它.
实现还应该处理应用程序的其他部分,这些部分尝试调用getSocket并返回undefined.基本上,getSocket应该永远不会返回undefined它应该等待连接然后返回this.socket.
我尝试了一些承诺,但我似乎无法找到一个优雅的解决方案.
我有一个基本的for循环,遍历一个值数组并在找到某个值时执行代码.该数组是从Google表格中获取的一列真或假值.
// array example: [[false], [false], [false],
// [true], [false], [false],
// [false], [true], [false],
// [false], [false], [false]]
for (i = 0; i < array.length; i++) {
if (array[i][0] === true) {
// execute code
}
Run Code Online (Sandbox Code Playgroud)
是否值得(性能方面)运行两个While循环来确定For循环应该从哪里开始以及它应该迭代多少次,或者它基本上是否相同?
var c = array.length - 1;
while (array[c][0] === false) {
c--;
}
var d = 0;
while (array[d][0] === false) {
d++;
}
var start_row = d+1;
var end_row = c+1;
for (i = start_row - 1; i < end_row; i++) …Run Code Online (Sandbox Code Playgroud) 我收到“ TypeError:无法读取未定义的属性'map'”。不知道我在哪里出错了。关于React,我还是很新,所以我不知道我是否缺少什么。当我尝试调用this.props.meals.map时,这给了我错误
export class Dashboard extends React.Component {
componentDidMount() {
this.props.dispatch(fetchProtectedData());
this.props.dispatch(retrieveDailyLogs())
.then(results => {
return this.props.dispatch(getDailyLogs(results));
})
}
getId(id) {
console.log('test');
this.props.dispatch(removeDay(id))
this.props.dispatch(retrieveDailyLogs())
.then(results => {
return this.props.dispatch(getDailyLogs(results));
});
}
render() {
const dailyLogs = this.props.meals.map((day, index) =>
<li className="card" key={index}>
<Card handleClick={(id) => this.getId(id)} {...day} />
</li>
)
return (
<section className="dashboard-container">
<h1>Dashboard</h1>
<Link className="log-day-btn" to="/dailylogs">Log Meals</Link>
<ul className="meal-list">
{dailyLogs}
</ul>
</section>
);
}
}
const mapStateToProps = state => ({
meals: state.dailyLogsReducer.dailyLogs
});
export default requiresLogin()(connect(mapStateToProps)(Dashboard));
Run Code Online (Sandbox Code Playgroud)
这是我的减速机,以防万一 …
我正在测试apiMiddleware调用它的辅助函数callApi。为了防止调用callApi将发出 API 调用的实际调用,我模拟了该函数。但是,它仍然被调用。
apiMiddleware.js
import axios from 'axios';
export const CALL_API = 'Call API';
export const callApi = (...arg) => {
return axios(...arg)
.then( /*handle success*/ )
.catch( /*handle error*/ );
};
export default store => next => action => {
// determine whether to execute this middleware
const callAPI = action[CALL_API];
if (typeof callAPI === 'undefined') {
return next(action)
}
return callAPI(...callAPI)
.then( /*handle success*/ )
.catch( /*handle error*/ );
}
Run Code Online (Sandbox Code Playgroud)
apiMiddleware.spec.js
import …Run Code Online (Sandbox Code Playgroud) 如何在 Polymer 3 中正确编写 debouncer ?
根据文档
Run Code Online (Sandbox Code Playgroud)import {microTask} from '@polymer/polymer/lib/utils/async.js'; import {Debouncer} from '@polymer/polymer/lib/utils/debounce.js'; // ... _debounceWork() { this._debounceJob = Debouncer.debounce(this._debounceJob, microTask, () => this._doWork()); }
这很好,但我需要配置一些时间。例如,这就是我在 Polymer 1 中的做法
this.debounce("scroll",function() {
this.$$("#scrollThreshold").clearTriggers();
}.bind(this), 400);
Run Code Online (Sandbox Code Playgroud)
和聚合物 2
this._debouncer = Polymer.Debouncer.debounce(
this._debouncer, // initially undefined
Polymer.Async.timeOut.after(400),
() => {
// some code
}
);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在 Polymer 3 中设置 400 毫秒去抖动
javascript ×9
arrays ×2
angular ×1
asynchronous ×1
axios ×1
d3.js ×1
es6-promise ×1
frontend ×1
http ×1
jestjs ×1
logoff ×1
loops ×1
mocking ×1
object ×1
polymer ×1
polymer-3.x ×1
reactjs ×1
redux ×1
time ×1
typescript ×1
vue.js ×1