我正在尝试设置接口中first定义的子属性,Name但是当这样做时,我总是得到一个错误,例如:
interface Name{
first: string,
last:string,
}
class Person{
private name:Name
public setName(firstName, lastName){
this.name.first = firstName;
this.name.last = lastName;
}
}
var person1 = new Person();
person1.setName('Tracy','Herrera');
Run Code Online (Sandbox Code Playgroud)
运行时我得到错误: Cannot set property 'first' of undefined
任何人都有想法解决这个问题?
我正在强制https访问我的网站,但必须加载一些内容http(例如视频内容不能通过https),但浏览器会因mixed-contents策略阻止请求.
经过几个小时的搜索,我发现我可以使用Content-Security-Policy,但我不知道如何允许混合内容.
<meta http-equiv="Content-Security-Policy" content="????">
Run Code Online (Sandbox Code Playgroud) 我正在开发网络应用程序,我使用Facebook分享我的内容,我面临的问题是当有人点击Facebook上发布的链接有时它会在应用程序浏览器中使用Facebook打开并导致许多问题.
有没有办法阻止Facebook使用我的链接并使用设备默认浏览器?
我想知道如何总是将一些数据附加到Eloquent模型而不需要它,例如在获取Posts表单数据库时我想将每个用户的用户信息附加为:
{
id: 1
title: "My Post Title"
body: "Some text"
created_at: "2-28-2016"
user:{
id: 1,
name: "john smith",
email: "example@mail.com"
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Laravel Framework构建一个REST api,我想要一种方法来强制API始终使用JSON进行响应,而不是通过以下方式执行此操作:
return Response::json($data);
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望每个响应都是JSON.有没有一个好方法呢?
更新:即使在未找到异常的异常情况下,响应也必须是JSON.
我需要知道如何检查变量是数组还是对象
var arr = ['foo', 'bar'];
var obj = {
0: 'foo',
1: 'bar'
}
document.write('arr is an: ' + typeof arr + ', obj is an: ' + typeof obj)
// The result is always:
// arr is an: object, obj is an: object
Run Code Online (Sandbox Code Playgroud)
有什么办法可以区分这两种类型吗?
我需要强制类实现一些方法,例如onCreate()在其他语言中,php我们可以看到类似的东西:
<?php
// Declare the interface 'Movement'
interface MovementEvents
{
public function onWalk($distance);
}
// Declare the abstract class 'Animal'
abstract class Animal implements MovementEvents{
protected $energy = 100;
public function useEnergy($amount){
$energy -= $amount;
}
}
class Cat extends Animal{
// If I didn't implement `onWalk()` I will get an error
public function onWalk($distance){
$amount = $distance/100;
$this->useEnergy($amount)
}
}
?>
Run Code Online (Sandbox Code Playgroud)
请注意,在我的示例中,如果我没有实现onWalk()代码将无法工作,您将收到错误,但是当我在TypeScript下面执行相同操作时:
// Declare the interface 'Movement'
interface MovementEvents
{
onWalk: …Run Code Online (Sandbox Code Playgroud) 我要做的是将每篇文章的评论附加到文章对象,但问题是我每次都需要请求不同数量的评论.
由于某种原因,我需要使用mutators,因为有时我要求50篇文章,我不想循环结果并附加注释.
那么是否可以执行以下操作以及如何传递额外参数.
这个型号:
class Article extends Model
{
protected $appends = ['user', 'comments', 'media'];
public function getCommentsAttribute($data, $maxNumberOfComments = 0)
{
// I need to set maxNumberOfComments
return $this->comments()->paginate($maxNumberOfComments);
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
class PostsController extends Controller
{
public function index()
{
//This will automatically append the comments to each article but I
//have no control over the number of comments
$posts = Post::user()->paginate(10);
return $posts;
}
}
Run Code Online (Sandbox Code Playgroud)
我不想做的是:
class PostsController extends Controller
{
public function index()
{ …Run Code Online (Sandbox Code Playgroud) 在不使用任何外部库的情况下,如何在使用之前等待脚本加载.
在我的情况下,我正在使用以下命令加载脚本:
(function (w,d,t,s,e,r) {
e = d.createElement(o);
r = d.getElementsByTagName(o)[0];
e.async = 1;
e.src = g;
r.parentNode.insertBefore(e, r)
})(window, document, 'script', '//mydomain.com/path/to/script.js');
Run Code Online (Sandbox Code Playgroud)
然后:
// then later I want to use some code form the script:
var obj = new classFromTheInjectedScript();
Run Code Online (Sandbox Code Playgroud)
有没有等待脚本加载然后开始使用它?
注意:我有一种方法可以在我想要加载的脚本中触发事件,然后听到它,如下所示,但这是一个好主意吗?
(function(w,d){
document.addEventListener('scriptLoadedCustomEvent',onScriptReady);
function onScriptReady(){
// what I need to do goes here!
}
})(window,document);
Run Code Online (Sandbox Code Playgroud) 我使用dotnet核心框架创建了一个CLI工具,并希望从控制台以如下方式运行它:
$ candyapp --arg1 some-value
Run Code Online (Sandbox Code Playgroud)
我现在通过使用以下命令转到dll文件所在的位置来运行它:
$ dotnet candyapp.dll candyapp --arg1
Run Code Online (Sandbox Code Playgroud)
谁能帮助我安装应用程序并在Mac上使用它?
我Typescript目前正在使用一个项目进行开发,但是我正在编译时遇到问题Typescript,然后使用将它们连接起来Gulp。
var gulp = require('gulp');
var ts = require('gulp-typescript');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
gulp.task('default', function () {
gulp.src('vendor/**/*.js')
// I want to add ts files then combile and concat
.gulp.src('src/**/*.ts')
.pipe(sourcemaps.init())
.pipe(ts({
noImplicitAny: true,
out: 'output.js'
}))
.pipe(concat('all.js'))
.pipe(uglify());
.pipe(sourcemaps.write('/'))
.pipe(gulp.dest('./dist/'));
});
Run Code Online (Sandbox Code Playgroud)
换句话说,我需要做的是:
JavaScript库。Typescripts。Typescripts与源映射一起编译。JavaScript。更新资料
或只是一种方法,以确保在
TypeScript之前将结果连接在一起之前已编译JavaScript。
我需要在视频JS中创建一个自定义事件,该事件会在播放过程中的某个时间点触发。
例如:
player.on('timeupdate',function(){
if(player.currentTime> 50){
// trigger some custom event like 'played50Second'
}
});
Run Code Online (Sandbox Code Playgroud)
然后我想做:
player.on('played50Second',function(evt){
console.log('Great Job, you played 50 second of the video!!!')
});
Run Code Online (Sandbox Code Playgroud)
我该如何实现?