我有一个自定义挂钩,它将检查您是否已登录,如果未登录,则将您重定向到登录页面。这是我的钩子的伪实现,假设您尚未登录:
import { useRouter } from 'next/router';
export default function useAuthentication() {
if (!AuthenticationStore.isLoggedIn()) {
const router = useRouter();
router.push('/login');
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用这个钩子时,我收到以下错误:
错误:找不到路由器实例。您应该只在应用程序的客户端内使用“next/router”。https://err.sh/vercel/next.js/no-router-instance
我检查了错误中的链接,但这并没有多大帮助,因为它只是告诉我将语句移至push渲染函数。
我也尝试过这个:
// My functional component
export default function SomeComponent() {
const router = useRouter();
useAuthentication(router);
return <>...</>
}
// My custom hook
export default function useAuthentication(router) {
if (!AuthenticationStore.isLoggedIn()) {
router.push('/login');
}
}
Run Code Online (Sandbox Code Playgroud)
但这只会导致同样的错误。
有什么方法可以允许在 Next.js 中的 React 组件之外进行路由吗?
我查看了CloudWatch Logs 的文档,但找不到任何方法来手动从log stream.
删除数据的唯一方法是:
log stream,但这将删除所有数据(不仅仅是我们要删除的数据)有人有删除特定日志记录的经验吗?
amazon-web-services amazon-cloudwatch amazon-cloudwatchlogs aws-cloudwatch-log-insights
我正在尝试消除动作中的任何内容,它以一种或另一种方式被吞没......
拿这个(伪)代码:
import { debounce } from "lodash";
const actions = {
debounceSomeLogging ({ dispatch }, text) {
console.log("Outside debounced function.");
debounce(function() {
console.log("Inside debounced function.");
dispatch("doRealThing");
}, 1000);
},
doRealThing({ commit }) {
// Whatever
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用该操作时,我看到了Outside debounced function,但我看不到其他日志记录并且其他操作没有被触发。
任何人都有这方面的经验,可以指出我正确的方向吗?
我有一个旧的代码库,需要使用 Java 8 重构,所以我有一个接口,它告诉我当前的站点是否支持该平台。
public interface PlatformSupportHandler {
public abstract boolean isPaltformSupported(String platform);
}
Run Code Online (Sandbox Code Playgroud)
我有多个类实现它,每个类都支持不同的平台。
一些实现类是:
@Component("bsafePlatformSupportHandler")
public class BsafePlatoformSupportHandler implements PlatformSupportHandler {
String[] supportedPlatform = {"iPad", "Android", "iPhone"};
Set<String> supportedPlatformSet = new HashSet<>(Arrays.asList(supportedPlatform));
@Override
public boolean isPaltformSupported(String platform) {
return supportedPlatformSet.contains(platform);
}
}
Run Code Online (Sandbox Code Playgroud)
另一个实现:
@Component("discountPlatformSupportHandler")
public class DiscountPlatoformSupportHandler implements PlatformSupportHandler{
String[] supportedPlatform = {"Android", "iPhone"};
Set<String> supportedPlatformSet = new HashSet<>(Arrays.asList(supportedPlatform));
@Override
public boolean isPaltformSupported(String platform) {
return supportedPlatformSet.contains(platform);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的过滤器中运行时,我得到了我想要的所需 bean:
platformSupportHandler = (PlatformSupportHandler) ApplicationContextUtil
.getBean(subProductType + Constants.PLATFORM_SUPPORT_HANDLER_APPEND); …Run Code Online (Sandbox Code Playgroud) 通常不鼓励流操作的行为参数的副作用,因为它们通常会导致无意中违反无状态要求,以及其他线程安全危险。
这是否包括将流的元素保存到数据库?
想象一下以下(伪)代码:
public SavedCar saveCar(Car car) {
SavedCar savedCar = this.getDb().save(car);
return savedCar;
}
public List<SavedCars> saveCars(List<Car> cars) {
return cars.stream()
.map(this::saveCar)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
与此实现相反的不良影响是什么:
public SavedCar saveCar(Car car) {
SavedCar savedCar = this.getDb().save(car);
return savedCar;
}
public List<SavedCars> saveCars(List<Car> cars) {
List<SavedCars> savedCars = new ArrayList<>();
for (Cat car : cars) {
savedCars.add(this.saveCar(car));
}
return savedCars.
}
Run Code Online (Sandbox Code Playgroud) docker-compose.yml文件的命名约定是什么?它应该总是这样命名docker-compose-jenkins.yml吗?还是我可以将其重命名为例如?
我正在使用 Typescript 编写应用程序,并希望使用ts-sinon创建单元测试。
在他们的自述文件中,他们声明您可以像这样存根方法:
import * as sinon from 'ts-sinon'
class Test {
method() { return 'original' }
}
const test = new Test();
const testStub = sinon.stubObject<Test>(test);
testStub.method.returns('stubbed');
expect(testStub.method()).to.equal('stubbed');
Run Code Online (Sandbox Code Playgroud)
但是这段代码给了我这个错误:
类型 '() => string' 上不存在属性 'returns'。
我究竟做错了什么?
我正在使用 CloudFormation 设置 CloudFront,但我需要配置该Headers属性的ForwardedValues属性。它应该以转发所有标头的方式进行设置。
我在ForwardedValues文档页面和与基于请求标头缓存内容相关的链接页面上都找不到如何执行此操作。
这是该Header属性的 CloudFormation“路径” :
someCloudFrontDistributionName:
Type: AWS::CloudFront::Distribution
Properties:
CacheBehaviors:
ForwaredValues:
Headers:
- # What to put here that will allow all Headers to be forwarded?
Run Code Online (Sandbox Code Playgroud) 我们能否确定在 java lambda 中调用的方法仅在 lambda 本身执行时调用,而不是提前调用?
如果你看一下我的代码:
StringBuilder myStringBuilder = new StringBuilder("my result");
Supplier<String> mySupplier = () -> "result: " + myStringBuilder.toString();
myStringBuilder.append(", after appending.");
System.out.println(mySupplied.get());
Run Code Online (Sandbox Code Playgroud)
我们能否 100% 确定结果始终是:
结果:我的结果,附加后
并且从来不只是
结果:我的结果
java ×4
java-8 ×3
javascript ×2
spring ×2
aws-cloudwatch-log-insights ×1
docker ×1
git ×1
java-stream ×1
lambda ×1
lodash ×1
maven ×1
mocha.js ×1
next-router ×1
next.js ×1
reactjs ×1
side-effects ×1
sinon ×1
spring-boot ×1
typescript ×1
vue.js ×1
vuejs2 ×1
vuex ×1
vuex-modules ×1