我有一个 Angular 6+ 应用程序,它被配置为使用Angular Universal来利用服务器端渲染。
我还使用TransferState来避免服务器和客户端应用程序上的重复 API 调用。
我的 angular 应用程序中的身份验证基于令牌。
问题出在用户第一次打开我的 Web 应用程序时,这导致为未经身份验证的用户呈现 index.html,而用户实际上已登录,但没有机会将令牌传输到服务器。因此,当客户端应用程序与服务器应用程序交换时,由于浏览器的localStorage/sessionStorage 中存在令牌,因此需要再次调用API。
我使用 node.js 和 express.js 来实现服务端渲染。
我认为解决方案是使用会话和 cookie。这需要我做很多工作,因为我不熟悉 node.js 来处理会话/cookie。有什么快速简便的解决方案吗?
我有一个基于回调的 API,如下所示:
class CallbackApi {
fun addListener(callback: Callback) {
// todo
}
fun removeListener(callback: Callback) {
// todo
}
interface Callback {
fun onResult(result: Int)
}
}
Run Code Online (Sandbox Code Playgroud)
以及将 API 转换为冷热流的扩展函数:
fun CallbackApi.toFlow() = callbackFlow<Int> {
val callback = object : CallbackApi.Callback {
override fun onResult(result: Int) {
trySendBlocking(result)
}
}
addListener(callback)
awaitClose { removeListener(callback) }
}
Run Code Online (Sandbox Code Playgroud)
您介意建议如何编写一个单元测试来确保 API 正确转换为热流吗?
这是我的尝试。经过反复试验,我想出了这个解决方案。
@Test
fun callbackFlowTest() = runBlocking {
val callbackApi = mockk<CallbackApi>()
val callbackSlot = slot<CallbackApi.Callback>()
every { callbackApi.addListener(capture(callbackSlot)) } …Run Code Online (Sandbox Code Playgroud) 我需要将导航菜单项对齐到右侧.我阅读了很多文章,问题和答案,但我找不到我的项目中的错误.这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="right">
<include
layout="@layout/content_activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_activity_home"
app:menu="@menu/activity_home_drawer" />
Run Code Online (Sandbox Code Playgroud)
这是activity_home_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_account"
android:icon="@drawable/ic_profile"
android:title="???? ??????" />
<item
android:id="@+id/menu_logout"
android:icon="@drawable/ic_sign_out"
android:title="???? ?? ????" />
</group>
Run Code Online (Sandbox Code Playgroud)
我可以让抽屉本身从右边打开,但NavigationView的元素仍然是从左到右.你可以在这里看到结果:
如您所见,菜单项不是从右到左.如何让他们rtl?
我知道使用Android O,现在我们可以阅读短信验证而无需READ_SMS权限.可以使用createAppSpecificSmsToken API完成.
但我需要一个完整的示例来演示整个SMS验证例程.
我需要声明一个类型,以便从其属性类型中删除未定义的类型。
假设我们有:
type Type1{
prop?: number;
}
type Type2{
prop: number | undefined;
}
type Type3{
prop: number;
}
Run Code Online (Sandbox Code Playgroud)
我需要定义一个称为的通用类型NoUndefinedField<T>,以NoUndefinedField<Type1>提供与相同的类型Type3和与相同的类型NoUndefinedField<Type2>。
我试过了
type NoUndefinedField<T> = { [P in keyof T]: Exclude<T[P], null | undefined> };
Run Code Online (Sandbox Code Playgroud)
但这仅适用于Type2。
我有一个组件,它根据客户端设备大小切换组件的模板。组件代码为:
import {Component} from '@angular/core';
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
@Component({
selector: 'ui-switcher',
template: `
<ng-content *ngIf="isSmall" select="mobile"></ng-content>
<ng-content *ngIf="!isSmall" select="web"></ng-content>
`
})
export class UiSwitcherComponent {
public isSmall: boolean;
constructor(breakpointObserver: BreakpointObserver) {
breakpointObserver.observe([Breakpoints.Small, Breakpoints.XSmall]).subscribe(result => {
this.isSmall = result.matches;
});
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样使用它:
<ui-switcher>
<web>
<!-- some commented details -->
<input class="form-control mr-2" #searchInput
type="text" (keyup)="this.search(searchInput.value)">
</web>
<mobile>
<!-- some commented details -->
<input class="form-control" #searchInput
type="text" (keyup)="this.search(searchInput.value)">
</mobile>
</ui-switcher>
Run Code Online (Sandbox Code Playgroud)
在移动尺寸中,一切正常,但在桌面尺寸中,传递给search(value)函数的值始终为空字符串。
当我调试应用程序时,似乎#searchInputtemplateref 工作不正常(它引用的元素的值始终为空)。
为什么模板引用不能正常工作?
android ×2
angular ×2
kotlin ×1
ng-content ×1
node.js ×1
null ×1
types ×1
typescript ×1
undefined ×1
unit-testing ×1
xml ×1