显示模板中相应元素后获取@ViewChild最优雅的方法是什么?以下是一个例子.也有Plunker可用.
模板:
<div id="layout" *ngIf="display">
<div #contentPlaceholder></div>
</div>
Run Code Online (Sandbox Code Playgroud)
零件:
export class AppComponent {
display = false;
@ViewChild('contentPlaceholder', {read: ViewContainerRef}) viewContainerRef;
show() {
this.display = true;
console.log(this.viewContainerRef); // undefined
setTimeout(()=> {
console.log(this.viewContainerRef); // OK
}, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个默认隐藏其内容的组件.当有人调用@ViewChild方法时,它变得可见.但是,在角度2变化检测完成之前,我无法参考show().我通常将所有必需的操作包装成viewContainerRef如上所示.有更正确的方法吗?
我知道有一个选项setTimeout(()=>{},1),但它会导致太多无用的通话.
我刚刚安装了Erlang 19.0,然后安装了Rabbitmq Server 3.6.3.操作系统 - Windows 10.然后我安装了rabbitmq_management插件,然后我启动了rabbitmq-server.我可以成功登录管理控制台.问题是,当我去队列时,我得到错误:
得到响应代码500 with body {"error":"JSON encode error:{bad_term,#{error_logger => true,kill => true,size => 0}}","reason":"while encoding:\n [ {total_count,1},\n {item_count,1},\n {filtered_count,1},\n {page,1},\n {page_size,100},\n {page_count,1},\n {items ,\n [[{memory,22048},\n {减少,6633},\n {reductions_details,[{rate,0.0}]},\n {messages,0},\n {messages_details,[{rate, 0.0}]},\n {messages_ready,0},\n {messages_ready_details,[{rate,0.0}]},\n {messages_unacknowledged,0},\n {messages_unacknowledged_details,[{rate,0.0}]},\n {idle_since,<< \"2016-07-08 20:55:04 \">>},\n {consumer_utilisation,''},\n {policy,''},\n {exclusive_consumer_tag,''} ,\n {消费者,1},\n {recoverable_slaves,''},\n {state,running},\n {减少,6633},\n {garbage_collection,\n [{max_heap_size,#{error_logger => true,kill => true,size => 0}},\n {min_bin_vheap_size,46422},\n {min_heap_size,233},\n {fullsweep_after,65535},\n {minor_gcs,3}]},\n {messages_ram,0},\n {messages_ready_ram,0},\n {messages_unacknowledg ed_ram,0},\n {messages_persistent,0},\n {message_bytes,0},\n {message_bytes_ready,0},\n {message_bytes_unacknowledged,0},\n {message_bytes_ram,0},\n {message_bytes_persistent, 0},\n {head_message_timestamp,''},\n {disk_reads,0},\n {disk_writes,0},\n {backing_queue_status,\n {struct,\n [{mode,default},\n …
我在spring https://spring.io/guides/gs/messaging-stomp-websocket/之后用spring 4,STOMP和sock.js做了一个简单的web socket通信.
现在我想将其升级为简单聊天.我的问题是,当用户订阅新的聊天室时,他应该通过消息.我不知道如何捕捉他订阅时向他发送消息列表的那一刻.
我尝试使用@MessageMapping注释,但没有取得任何成功:
@Controller
public class WebSocketController {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping("/chat/{chatId}")
public void chat(ChatMessage message, @DestinationVariable String chatId) {
messagingTemplate.convertAndSend("/chat/" + chatId, new ChatMessage("message: " + message.getText()));
}
@SubscribeMapping("/chat")
public void chatInit() {
System.out.println("worked");
int chatId = 1; //for example
messagingTemplate.convertAndSend("/chat/" + chatId, new ChatMessage("connected"));
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了:
@Controller
public class ApplicationEventObserverController implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println(applicationEvent);
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但捕获所有可能的事件,我认为这不是一个好习惯.
所以,我的问题可以改写:如何在用户提交给某些人时发送初始数据?
有没有办法做到这一点?
客户端:
function connectWebSocket() {
var socket = new SockJS('/socket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected");
});
}
Run Code Online (Sandbox Code Playgroud)
服务器端并不重要.执行上面的代码后,我需要知道我的会话ID.
这与将日志文件名设置为在Log4j中包括当前日期的问题相同,但是如何将其应用于slf4j附带的Spring Boot?
application.properties
spring.application.name=keywords
logging.file=logs/${spring.application.name}.log
Run Code Online (Sandbox Code Playgroud) 我想重用 Spring 生产上下文配置,但用另一个 bean 替换一些 bean。如果我想用模拟覆盖它们,我会使用@MockBean,它完全满足我的需要(覆盖 bean),但不允许我自己配置一个新的 bean。
我知道还有另一种使用方法,@ContextConfiguration但对我来说似乎太冗长了。
谢谢。
我已经在application.properties中设置了spring.datasource。*:
spring.datasource.url=jdbc:h2:./data/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
然后我配置了JdbcTemplate Bean
@Bean
@Autowired
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Run Code Online (Sandbox Code Playgroud)
但是当我启动应用程序时,我在控制台中看到了
Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
Run Code Online (Sandbox Code Playgroud)
而不是我的设置。为什么?
spring database-connection spring-jdbc properties-file spring-boot
昨天我安装了 Ubuntu,然后安装了 Intellij IDEA 并从 Windows 版本导入了我的设置。现在,如果我想从 github 导入我的项目,则会出现错误:“无法运行程序“C:/Program Files (x86)/Git/cmd/git.exe”:error=2,没有这样的文件或目录。” 我该如何修复它?
spring ×5
java ×3
spring-boot ×2
stomp ×2
websocket ×2
angular ×1
git ×1
github ×1
linux ×1
logging ×1
rabbitmq ×1
slf4j ×1
sockjs ×1
spring-jdbc ×1
spring-mvc ×1
spring-test ×1
ubuntu ×1