小编Mar*_*her的帖子

如何展平嵌套的结果?

我正在使用一个第三方库,该库提供了我必须“按原样”使用的基于树的数据结构。API 返回Result<T, Error>. 我必须进行一些顺序调用并将错误转换为我的应用程序的内部错误。

use std::error::Error;
use std::fmt;

pub struct Tree {
    branches: Vec<Tree>,
}

impl Tree {
    pub fn new(branches: Vec<Tree>) -> Self {
        Tree { branches }
    }

    pub fn get_branch(&self, id: usize) -> Result<&Tree, TreeError> {
        self.branches.get(id).ok_or(TreeError {
            description: "not found".to_string(),
        })
    }
}

#[derive(Debug)]
pub struct TreeError {
    description: String,
}

impl Error for TreeError {
    fn description(&self) -> &str {
        self.description.as_str()
    }
}

impl fmt::Display for TreeError {
    fn fmt(&self, f: &mut fmt::Formatter) …
Run Code Online (Sandbox Code Playgroud)

error-handling flatten rust

8
推荐指数
1
解决办法
3944
查看次数

如何从不同的函数发出 Flow 值?Kotlin 协程

我有一个流程:

val myflow = kotlinx.coroutines.flow.flow<Message>{}
Run Code Online (Sandbox Code Playgroud)

并想用函数发出值:

override suspend fun sendMessage(chat: Chat, message: Message) {
    myflow.emit(message)
}
Run Code Online (Sandbox Code Playgroud)

但是编译器不允许我这样做,有什么解决方法可以解决这个问题吗?

kotlin kotlin-coroutines kotlin-flow

8
推荐指数
2
解决办法
6361
查看次数

使用当前 vue 组件的方法作为默认 prop 值

我将把函数作为属性传递给 Vue 组件。让它成为函数必须被调用@click。但出于某些原因,我想保留组件的默认行为。默认行为并不像我想要的那么简单,我将method用作默认功能。因为默认行为需要来自组件状态的数据(道具、数据、其他方法等等)。

有办法吗?

我附上了这个例子。预期行为:

按钮works fine应该产生警报You are welcome!
按钮nope应该产生警报You are welcome as well!但什么也不做。

Vue.component('welcome-component', {
  template: '#welcome-component',
  props: {
    name: {
      type: String,
      required: true,
    },
    action: {
      type: Function,
      required: false,
      default: () => { this.innerMethod() },
    },
  },
  methods: {
    innerMethod() {
      alert("You are welcome as well!");
    }
  }
});


var app = new Vue({
  el: "#app",
  methods: {
    externalMethod() {
      alert("You are welcome!");
    }
  } …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js vue-component vuejs2

6
推荐指数
2
解决办法
3176
查看次数

模型映射器中的延迟初始化异常

ModelmapperLazyInitializationException在从实体转换为 dto 时给出。

有什么办法可以禁用它。如果我modelmapper.map在事务块内部调用它工作正常,但它正在加载我根本不想要的所有惰性对象。我想要如果懒惰然后根本不加载它。

转换器 org.modelmapper.internal.converter.MergingCollectionConverter@6a51c12e 未能将 org.hibernate.collection.internal.PersistentSet 转换为 java.util.Set。

引起:org.modelmapper.MappingException:ModelMapper 映射错误:

1) 无法从中获取价值 com.app.flashdiary.entity.Vendor.getApproved()

引起:org.hibernate.LazyInitializationException:无法初始化代理 [com.app.flashdiary.entity.Vendor#1] - 在 org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169) 没有会话

java hibernate modelmapper

5
推荐指数
1
解决办法
3071
查看次数

变形为 Laravel Nova

我有 MorphTo - MorphMany 关系,这样地址可以属于用户或组织。我不断收到错误消息

调用未定义的方法 App\HTTP\Resources\User::singularLabel()

我尝试使用静态方法,但这似乎没有帮助。文件的代码如下。

谢谢

地址.php 模型

public function addressable()
{
   return $this->morphTo('addressable');
}
public function getHashidsConnection()
{
    return 'address';
}
Run Code Online (Sandbox Code Playgroud)

User.php(模型)

public function organisation()
{
   return $this->belongsTo('App\Organisation');
}

public function addresses()
{
   return $this->morphMany('App\Address', 'addressable');
}
Run Code Online (Sandbox Code Playgroud)

地址.php(资源)

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('line_1'),
        Text::make('line_2'),
        Text::make('line_3'),
        Text::make('post_code'),
        Boolean::make('is_primary'),

        MorphTo::make('Addressable')->types([ 
            User::class,
            Organisation::class,
        ])
    ];
}

public static function singularLabel()
{
    return Address::class;
}
Run Code Online (Sandbox Code Playgroud)

用户.php(资源)

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Gravatar::make(),

        Text::make('Name') …
Run Code Online (Sandbox Code Playgroud)

php relationship laravel eloquent

4
推荐指数
1
解决办法
2461
查看次数

如何使选项元素只读但不禁用选择元素

我尝试将 readonly 属性设置为 true,但它不起作用,因为我仍然可以从下拉列表中选择值。

<tr>
    <td>
        <select name="type" tabindex="0">
            <option value=""></option>
            <option value="first">first</option> 
            <option value="second">second</option>
        </select> 
    </td> 
</tr>
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

1
推荐指数
1
解决办法
5789
查看次数