我正在尝试构建一个用 TypeScript 制作的库。我paths在tsconfig.json.
我的tsconfig.json看起来像这样:
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"target": "es2015",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"experimentalDecorators": true,
"sourceMap": true,
"lib": [
"esnext",
"dom"
],
"strict": true,
"sourceRoot": "./src",
"rootDir": "./src",
"outDir": "build",
"declaration": true,
"declarationDir": "build",
"baseUrl": "./src",
"noUnusedLocals": true,
"paths": {
"modules/*": [
"modules/*"
],
"common/*": [
"common/*"
]
}
},
"exclude": [
"node_modules",
"build"
]
}
Run Code Online (Sandbox Code Playgroud)
我的汇总配置如下所示:
import typescript from 'rollup-plugin-typescript2'
export default {
entry: "./src/index.ts",
output: { …Run Code Online (Sandbox Code Playgroud) 我正在尝试对我的其他本地主机服务器进行 REST Api 调用,我已经提供了此服务:
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {GlobalService} from '../app.service';
@Injectable()
export class AuthenticationService {
constructor(private _globals: GlobalService) {}
constructor(private _http: Http) {}
globals = this._globals.getGlobals()
getAuthenticationData() {
this._http.get(globals.apiURL)
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的组件中调用它:
import { Component } from 'angular2/core';
import {AuthenticationService} from '../services/authentication.service';
@Component({
selector: 'wd-header';
templateUrl: 'app/templates/header.html'
providers: [AuthenticationService]
})
export class HeaderComponent {
constructor(private _authenticationService: AuthenticationService) {}
authentication = this._authenticationService.getAuthenticationData()
}
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,Angular 声称 Http 未定义:
异常: HeaderComponent 实例化期间出错!。angular2.dev.js:22911:9
原始异常:类型错误:this._http 未定义
我究竟做错了什么?
编辑以包含 main.ts: …
我想能够在angular2之外的变量发生变化时观察和更新.所以,假设我在外部javascript文件中有这个:
var test = 1;
Run Code Online (Sandbox Code Playgroud)
如何将此变量绑定到组件中的属性?
@Component({
...
})
export class MyComponent {
watchy = window.test;
}
Run Code Online (Sandbox Code Playgroud)
根据这个答案,显然这应该是有用的.
但事实并非如此.如果我在控制台中更改变量,则变量不会更新显示在模板中.我错过了什么吗?
我正在尝试制作自己的Tabs组件,以便我可以在我的应用中使用标签.但是,我似乎在尝试按类型提取我需要的子组件时遇到问题.
import React from 'react'
export class Tabs extends React.Component {
render() {
let children = this.props.children
let tablinks = React.Children.map(children, x => {
console.log(x.type.displayName) // Always undefined
if (x.type.displayName == 'Tab Link') {
return x
}
})
return (
<div className="tabs"></div>
)
}
}
export class TabLink extends React.Component {
constructor(props) {
super(props)
this.displayName = 'Tab Link'
}
render() {
return (
<div className="tab-link"></div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
<Tabs>
<TabLink path="tab1">Test</TabLink>
<TabLink path="tab2">Test2</TabLink>
</Tabs>
Run Code Online (Sandbox Code Playgroud)
我console.log永远不会返回"Tab Link",它总会返回 …
我正在尝试使用通用关系,我的模型如下所示:
class Post(models.Model):
# Identifiers
user = models.ForeignKey(User, unique=False, related_name = 'posts')
# Resource
resource_type = models.ForeignKey(ContentType)
resource_id = models.PositiveIntegerField()
resource = GenericForeignKey('resource_type', 'resource_id')
# Other
date_created = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True)
class Meta:
unique_together = ('resource_type', 'resource_id',)
Run Code Online (Sandbox Code Playgroud)
但是,在我的资源上,尝试使用' SomeResource.posts' 获取Post对象时,会发生以下异常:
无法将关键字“ content_type”解析为字段。选项包括:date_created,id,resource,resource_id,resource_type,resource_type_id,user,user_id
content_type当我在我的名字resource_type上明确命名它时,为什么要寻找它GenericForeignKey?
我正在尝试通过查询集相关的另一个模型的计数来订购查询集。
class Post(models.Model):
...
class PostView(models.Model):
post = models.ForeignKey(Post, related_name="post_views")
Run Code Online (Sandbox Code Playgroud)
现在,我认为这会起作用:
Post.objects.all().order_by('post_views')
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
/api/posts/?order_by=views 处的 AttributeError
'ManyToOneRel' 对象没有属性 'attname'
我究竟做错了什么?
是否可以从Django Rest Framework中的序列化对象获取字段值?像这样的东西:
serializer = PostSerializer(post)
print(serializer.title)
# 'foo title'
Run Code Online (Sandbox Code Playgroud)
对于在序列化程序中添加的字段,我需要这个,但不在模型上,就像登录用户喜欢帖子一样.
我想在使用Mutagen将它们保存到磁盘之前验证我的mp3.但是使用mutagen我只能打开一个文件,如果它在磁盘上.有没有解决的办法?我希望能够做到这一点:
files = request.FILES
mp3 = files.get('mp3')
mp3_audio = MP3(mp3)
Run Code Online (Sandbox Code Playgroud)
给我错误:
TypeError: invalid file: <TemporaryUploadedFile: test.mp3 (audio/mpeg)>
Run Code Online (Sandbox Code Playgroud) 我正在尝试将文件上传到我的快速服务器。客户端代码如下所示:
axios.post('localhost:3030/upload/audio/', formData)
Run Code Online (Sandbox Code Playgroud)
在我的快递服务器中:
App.use(bodyParser.urlencoded({ extended: true }));
App.use(bodyParser.json());
App.post('/upload/audio/', function uploadAudio(req, res) {
let quality = ['320', '128'];
let file = req.body;
console.log(file)
res.send('Frick')
}
Run Code Online (Sandbox Code Playgroud)
但是,即使发送了 mp3 文件:
在req.body登录时(注意空对象)是空的:
如何在 Express.js 中获取 formData(和文件)?
element.scrollIntoView行为设置为smooth在 Chrome 中无法正常工作。当它仅用于调用堆栈中的一个元素时,它工作正常。但是如果它用于多个元素,则只有最后一个元素会实际滚动。
这在 Firefox 中运行良好。Chrome 中是否有解决方法?
const $ = (s) => document.querySelector(s)
const $$ = (s) => document.querySelectorAll(s)
const container = $(".container")
for (let i = 0; i < 2; i++) {
document.body.appendChild(container.cloneNode(true))
}
function scrollIntoView(behavior) {
for (const element of $$(".reveal")) {
element.scrollIntoView({
behavior,
block: "end"
})
}
}
$(".instant").addEventListener("click", () => scrollIntoView("instant"))
$(".smooth").addEventListener("click", () => scrollIntoView("smooth"))
$(".reset").addEventListener("click", () => {
for (const element of $$(".container")) {
element.scrollTo(0, 0)
}
})Run Code Online (Sandbox Code Playgroud)
.container {
max-height: calc(33vh …Run Code Online (Sandbox Code Playgroud)